测试类
- Person.java
后面的测试都是以Person对象的集合处理展开
1@Data
2@AllArgsConstructor
3@ToString
4class Person {
5 // 姓名
6 String name;
7 // 性别
8 String sex;
9 // 年纪
10 Integer age;
11 // 工资
12 Integer salary;
13}
循环 forEach
-
List循
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 25, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 48, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7
8personList.forEach(System.out::println);
执行结果:
1Person(name=张三, sex=男, age=17, salary=1000)
2Person(name=李四, sex=男, age=25, salary=5000)
3Person(name=王五, sex=女, age=36, salary=3000)
4Person(name=赵六, sex=女, age=48, salary=7000)
5Person(name=多多, sex=男, age=100, salary=9000)
-
Map循环
1Map<String, List<Person>> sexMap = new HashMap<>();
2sexMap.put("男", Arrays.asList(
3 new Person("张三", "男", 17, 1000),
4 new Person("李四", "男", 25, 5000)
5));
6sexMap.put("女", Arrays.asList(
7 new Person("王五", "女", 36, 3000),
8 new Person("赵六", "女", 48, 7000)
9));
10sexMap.forEach((key, value) -> {
11 System.out.println(key);
12 System.out.println(value);
13});
执行结果:
1女
2[Person(name=王五, sex=女, age=36, salary=3000), Person(name=赵六, sex=女, age=48, salary=7000)]
3男
4[Person(name=张三, sex=男, age=17, salary=1000), Person(name=李四, sex=男, age=25, salary=5000)]
结果收集 collect
- 收集集合流中的所有人的姓名,拼接返回字符串
1List<Person> personList = new ArrayList<>();
2personList.add(new Person("张三", "男", 17, 1000));
3personList.add(new Person("李四", "男", 36, 5000));
4personList.add(new Person("王五", "女", 36, 3000));
5personList.add(new Person("张三", "女", 17, 7000));
6personList.add(new Person("多多", "男", 100, 9000));
7personList.add(new Person("哈哈", "男", 36, 4000));
8String nameStr = personList.stream().map(Person::getName).collect(Collectors.joining(","));
9System.out.println(nameStr);
执行结果:
1张三,李四,王五,张三,多多,哈哈
映射 map
映射原始集合,结合collect方法生成新的集合
- 获取所有人的姓名集合
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 25, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 48, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7
8List<String> nameList = personList.stream().map(Person::getName).collect(Collectors.toList());
9System.out.println(nameList);
执行结果:
1[张三, 李四, 王五, 赵六, 多多]
过滤 filter
找出列表中满足条件元素
- 找出年龄大于30岁的人
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 25, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 48, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7
8List<Person> personListResult = personList.stream().filter(person -> person.getAge() > 30).collect(Collectors.toList());
9System.out.println(personListResult);
执行结果:
1[Person(name=王五, sex=女, age=36, salary=3000),
2Person(name=赵六, sex=女, age=48, salary=7000),
3Person(name=多多, sex=男, age=100, salary=9000)]
分组 Collectors.goupingBy
根据对象中的指定名称进行分组,返回一个Map<分组字段,对象集合>
- 根据性别分组
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 25, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 48, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7
8Map<String, List<Person>> sexMap = personList.stream().collect(Collectors.groupingBy(Person::getSex));
9System.out.println(sexMap);
执行结果:
1{
2女=[Person(name=王五, sex=女, age=36, salary=3000), Person(name=赵六, sex=女, age=48, salary=7000)],
3男=[Person(name=张三, sex=男, age=17, salary=1000), Person(name=李四, sex=男, age=25, salary=5000), Person(name=多多, sex=男, age=100, salary=9000)]
4}
分组求和
- 根据性别分组,然后求出男女的工资总和
1List<Person> personList = new ArrayList<>();
2personList.add(new Person("张三", "男", 17, 1000));
3personList.add(new Person("李四", "男", 36, 5000));
4personList.add(new Person("王五", "女", 36, 3000));
5personList.add(new Person("赵六", "女", 17, 7000));
6personList.add(new Person("多多", "男", 100, 9000));
7personList.add(new Person("哈哈", "男", 36, 4000));
8Map<String, Integer> salarySumMap = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.summingInt(Person::getSalary)));
9System.out.println(salarySumMap);
执行结果:
1{女=10000, 男=19000}
排序 sorted
- 根据年龄顺序排序
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 25, 3000));
5 personList.add(new Person("赵六", "女", 22, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7personList = personList.stream().sorted(Comparator.comparingInt(Person::getAge)).collect(Collectors.toList());
8System.out.println(personList);
执行结果:
1[
2Person(name=张三, sex=男, age=17, salary=1000),
3Person(name=赵六, sex=女, age=22, salary=7000),
4Person(name=王五, sex=女, age=25, salary=3000),
5Person(name=李四, sex=男, age=36, salary=5000),
6Person(name=多多, sex=男, age=100, salary=9000)]
- 根据年龄倒序排序
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 25, 3000));
5 personList.add(new Person("赵六", "女", 22, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7personList = personList.stream().sorted(Comparator.comparingInt(Person::getAge).reversed()).collect(Collectors.toList());
8System.out.println(personList);
执行结果:
1[
2Person(name=多多, sex=男, age=100, salary=9000),
3Person(name=李四, sex=男, age=36, salary=5000),
4Person(name=王五, sex=女, age=25, salary=3000),
5Person(name=赵六, sex=女, age=22, salary=7000),
6Person(name=张三, sex=男, age=17, salary=1000)]
多条件排序
- 年龄和工资进行排序
年龄相同,工资作为第二排序条件
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 17, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7 personList.add(new Person("哈哈", "男", 36, 4000));
8personList = personList.stream().sorted(
9 Comparator.comparingInt(Person::getAge).thenComparing(Person::getSalary)
10).collect(Collectors.toList());
11System.out.println(personList);
执行结果:
1[
2Person(name=张三, sex=男, age=17, salary=1000),
3Person(name=赵六, sex=女, age=17, salary=7000),
4Person(name=王五, sex=女, age=36, salary=3000),
5Person(name=哈哈, sex=男, age=36, salary=4000),
6Person(name=李四, sex=男, age=36, salary=5000),
7Person(name=多多, sex=男, age=100, salary=9000)]
去重 dictinct
- 找出所有不重复的年龄
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 17, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7 personList.add(new Person("哈哈", "男", 36, 4000));
8List<Integer> ages = personList.stream().map(Person::getAge).distinct().collect(Collectors.toList());
9System.out.println(ages);
执行结果:
1[17, 36, 100]
求和 sum
- 所有人的工资总和
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 17, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7 personList.add(new Person("哈哈", "男", 36, 4000));
8Integer salarySum = personList.stream().mapToInt(Person::getSalary).sum();
9System.out.println(salarySum);
执行结果:
129000
平均值 average
- 找出36岁年龄人群的平均工资
1List<Person> personList = new ArrayList<>();
2 personList.add(new Person("张三", "男", 17, 1000));
3 personList.add(new Person("李四", "男", 36, 5000));
4 personList.add(new Person("王五", "女", 36, 3000));
5 personList.add(new Person("赵六", "女", 17, 7000));
6 personList.add(new Person("多多", "男", 100, 9000));
7 personList.add(new Person("哈哈", "男", 36, 4000));
8Double average = personList.stream().filter(person -> person.age == 36).mapToInt(Person::getSalary).average().getAsDouble();
9System.out.println(average);
执行结果:
14000.0
最大值 max
- 找出工资最高的人
1List<Person> personList = new ArrayList<>();
2personList.add(new Person("张三", "男", 17, 1000));
3personList.add(new Person("李四", "男", 36, 5000));
4personList.add(new Person("王五", "女", 36, 3000));
5personList.add(new Person("赵六", "女", 17, 7000));
6personList.add(new Person("多多", "男", 100, 9000));
7personList.add(new Person("哈哈", "男", 36, 4000));
8Person salaryMaxPerson = personList.stream().max(Comparator.comparingInt(Person::getSalary)).get();
9System.out.println(salaryMaxPerson);
执行结果:
1Person(name=多多, sex=男, age=100, salary=9000)
最小值 min
1List<Person> personList = new ArrayList<>();
2personList.add(new Person("张三", "男", 17, 1000));
3personList.add(new Person("李四", "男", 36, 5000));
4personList.add(new Person("王五", "女", 36, 3000));
5personList.add(new Person("赵六", "女", 17, 7000));
6personList.add(new Person("多多", "男", 100, 9000));
7personList.add(new Person("哈哈", "男", 36, 4000));
8Person salaryMaxPerson = personList.stream().min(Comparator.comparingInt(Person::getSalary)).get();
9System.out.println(salaryMaxPerson);
执行结果:
1Person(name=张三, sex=男, age=17, salary=1000)
shimo.im/docs/gGQHg8… 《2020年最新Java架构师系统进阶资料免费领取》,可复制链接后用石墨文档 App 或小程序打开