Java8新特性利用流和Lambda表达式对List集合常用处理(java笔记)

最近在做项目的过程中经常会接触到 lambda 表达式,随后发现它基本上可以替代所有 for 循环,包括增强for循环。也就是我认为,绝大部分的for循环都可以用 lambda 表达式改写。

lambda表达式有它自己的优点:(1)简洁,(2)易并行计算。尤其适用于遍历结果,循环计算数值或者赋值的时候非常方便。

缺点: (1)若不用并行计算,很多时候计算速度没有比传统的 for 循环快。

   (2)不容易使用debug模式调试。

   (3)在 lambda 语句中直接强制类型转换不方便。

   (4)不可以在foreach中修改foreach外面的值。


下面是一些我再开发过程中经常使用过的表达式去处理list集合。

(1)先新建一个实体类

<code> 1 public class Person {
2
3 private String name;
4 private int age;
5 private int size;
6 public String getName() {
7 return name;
8 }
9 public void setName(String name) {
10 this.name = name;
11 }
12 public int getAge() {
13 return age;
14 }

15 public void setAge(int age) {
16 this.age = age;
17 }
18 public int getSize() {
19 return size;
20 }
21 public void setSize(int size) {
22 this.size = size;
23 }
24 @Override
25 public String toString() {
26 return "Person [name=" + name + ", age=" + age + ", size=" + size + "]";
27 }
28 public Person(String name, int age, int size) {
29 super();
30 this.name = name;
31 this.age = age;
32 this.size = size;
33 }
34 public Person() {
35 super();
36 // TODO Auto-generated constructor stub
37 }
38
39 }/<code>
Java8新特性利用流和Lambda表达式对List集合常用处理(java笔记)

(2)写主函数。剩下的说明会写在代码中

Java8新特性利用流和Lambda表达式对List集合常用处理(java笔记)

<code> 1 public class Jdk8Main {
2
3 public static void main(String[] args) {
4 List<person> list = new ArrayList<>();
5 Person p1 = new Person("张1",1,1);
6 Person p101 = new Person("张101",101,101);
7 Person p2 = new Person("张2",2,2);
8 Person p3 = new Person("张3",3,3);
9 Person p4 = new Person("张4",4,4);
10 Person p5 = new Person("张5",5,5);
11 Person p6 = new Person("张6",6,6);
12 list.add(p1);
13 list.add(p2);
14 list.add(p3);
15 list.add(p4);
16 list.add(p5);
17 list.add(p6);
18 list.add(p101);
19
20 /**
21 * 1.forEach()进行遍历集合
22 * item:可以是任意值。类似于for循环中的循环值
23 */
24 list.forEach(item->{
25 //设置值
26 item.setName(item.getName()+"测试");;
27 //输出语句
28 System.out.println(item.toString());
29 });
30
31 /**
32 * 2.stream()流操作
33 */
34 //2.1. 去重 distinct() 去重;collect(Collectors.toList())。封装成集合
35 List<person> distinctList = list.stream().distinct().collect(Collectors.toList());
36 //2.2 排序 sorted((第一个对象,第二个对象)->返回值) (升降序看是第几个对象与第几个对象比较)
37 List<person> sortedList = list.stream().sorted((o1,o2)->o1.getAge()-o2.getAge()).collect(Collectors.toList());
38 //2.3 过滤 , filter(item->{}) item为每一项。 按照自己的需求来筛选list中的数据

39 List<person> filterList = list.stream().filter(item->item.getAge()>3).collect(Collectors.toList());
40 //2.4 map(), 提取对象中的某一元素. 用每一项来获得属性(也可以直接用 对象::get属性())
41 List<string> mapList1 = list.stream().map(Person::getName).collect(Collectors.toList());
42 List<string> mapList2 = list.stream().map(item->item.getName()).collect(Collectors.toList());
43 //2.5 统计 sum() 。mapToDouble() 转换成double。还有其他类型转换。可以自己研究。
44 // max(),min(),average()
45 double sum = list.stream().mapToDouble(Person::getAge).sum();
46 //2.6 分组 Collectors.groupingBy(属性名)
47 Map<integer>> map = list.stream().collect(Collectors.groupingBy(Person::getAge));
48 //2.7 多重分组 Collectors.groupingBy(属性,Collectors.groupingBy(属性))
49 Map<string>>> map2 = list.stream().collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge())));
50 //2.8 分组并计算综合 Collectors.summarizingLong()
51 Map<string>> map3 = list.stream().collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge(),Collectors.summarizingLong(Person::getSize))));
52
53 /**
54 * 3. 集合比较的简写方式
55 */
56 list.sort((o1,o2)->{return o1.getAge()-o2.getAge();});
57 }
58 }/<string>/<string>/<integer>/<string>/<string>/<person>/<person>/<person>/<person>/<code>


分享到:


相關文章: