后面的测试都是以Person对象的集合处理展开
@Data
@AllArgsConstructor
@ToString
class Person {
// 姓名
String name;
// 性别
String sex;
// 年纪
Integer age;
// 工资
Integer salary;
}
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 25, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 48, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.forEach(System.out::println);
执行结果:
Person(name=张三, sex=男, age=17, salary=1000) Person(name=李四, sex=男, age=25, salary=5000) Person(name=王五, sex=女, age=36, salary=3000) Person(name=赵六, sex=女, age=48, salary=7000) Person(name=多多, sex=男, age=100, salary=9000)
Map<String, List<Person>> sexMap = new HashMap<>();
sexMap.put("男", Arrays.asList(
new Person("张三", "男", 17, 1000),
new Person("李四", "男", 25, 5000)
));
sexMap.put("女", Arrays.asList(
new Person("王五", "女", 36, 3000),
new Person("赵六", "女", 48, 7000)
));
sexMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
执行结果:
女 [Person(name=王五, sex=女, age=36, salary=3000), Person(name=赵六, sex=女, age=48, salary=7000)] 男 [Person(name=张三, sex=男, age=17, salary=1000), Person(name=李四, sex=男, age=25, salary=5000)]
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("张三", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
String nameStr = personList.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println(nameStr);
执行结果:
张三,李四,王五,张三,多多,哈哈
映射原始集合,结合collect方法生成新的集合
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 25, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 48, 7000));
personList.add(new Person("多多", "男", 100, 9000));
List<String> nameList = personList.stream().map(Person::getName).collect(Collectors.toList());
System.out.println(nameList);
执行结果:
[张三, 李四, 王五, 赵六, 多多]
找出列表中满足条件元素
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 25, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 48, 7000));
personList.add(new Person("多多", "男", 100, 9000));
List<Person> personListResult = personList.stream().filter(person -> person.getAge() > 30).collect(Collectors.toList());
System.out.println(personListResult);
执行结果:
[Person(name=王五, sex=女, age=36, salary=3000), Person(name=赵六, sex=女, age=48, salary=7000), Person(name=多多, sex=男, age=100, salary=9000)]
根据对象中的指定名称进行分组,返回一个Map<分组字段,对象集合>
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 25, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 48, 7000));
personList.add(new Person("多多", "男", 100, 9000));
Map<String, List<Person>> sexMap = personList.stream().collect(Collectors.groupingBy(Person::getSex));
System.out.println(sexMap);
执行结果:
{
女=[Person(name=王五, sex=女, age=36, salary=3000), Person(name=赵六, sex=女, age=48, salary=7000)],
男=[Person(name=张三, sex=男, age=17, salary=1000), Person(name=李四, sex=男, age=25, salary=5000), Person(name=多多, sex=男, age=100, salary=9000)]
}
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
Map<String, Integer> salarySumMap = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.summingInt(Person::getSalary)));
System.out.println(salarySumMap);
执行结果:
{女=10000, 男=19000}
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 25, 3000));
personList.add(new Person("赵六", "女", 22, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList = personList.stream().sorted(Comparator.comparingInt(Person::getAge)).collect(Collectors.toList());
System.out.println(personList);
执行结果:
[ Person(name=张三, sex=男, age=17, salary=1000), Person(name=赵六, sex=女, age=22, salary=7000), Person(name=王五, sex=女, age=25, salary=3000), Person(name=李四, sex=男, age=36, salary=5000), Person(name=多多, sex=男, age=100, salary=9000)]
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 25, 3000));
personList.add(new Person("赵六", "女", 22, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList = personList.stream().sorted(Comparator.comparingInt(Person::getAge).reversed()).collect(Collectors.toList());
System.out.println(personList);
执行结果:
[ Person(name=多多, sex=男, age=100, salary=9000), Person(name=李四, sex=男, age=36, salary=5000), Person(name=王五, sex=女, age=25, salary=3000), Person(name=赵六, sex=女, age=22, salary=7000), Person(name=张三, sex=男, age=17, salary=1000)]
年龄相同,工资作为第二排序条件
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
personList = personList.stream().sorted(
Comparator.comparingInt(Person::getAge).thenComparing(Person::getSalary)
).collect(Collectors.toList());
System.out.println(personList);
执行结果:
[ Person(name=张三, sex=男, age=17, salary=1000), Person(name=赵六, sex=女, age=17, salary=7000), Person(name=王五, sex=女, age=36, salary=3000), Person(name=哈哈, sex=男, age=36, salary=4000), Person(name=李四, sex=男, age=36, salary=5000), Person(name=多多, sex=男, age=100, salary=9000)]
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
List<Integer> ages = personList.stream().map(Person::getAge).distinct().collect(Collectors.toList());
System.out.println(ages);
执行结果:
[17, 36, 100]
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
Integer salarySum = personList.stream().mapToInt(Person::getSalary).sum();
System.out.println(salarySum);
执行结果:
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
Double average = personList.stream().filter(person -> person.age == 36).mapToInt(Person::getSalary).average().getAsDouble();
System.out.println(average);
执行结果:
4000.0
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
Person salaryMaxPerson = personList.stream().max(Comparator.comparingInt(Person::getSalary)).get();
System.out.println(salaryMaxPerson);
执行结果:
Person(name=多多, sex=男, age=100, salary=9000)
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", 17, 1000));
personList.add(new Person("李四", "男", 36, 5000));
personList.add(new Person("王五", "女", 36, 3000));
personList.add(new Person("赵六", "女", 17, 7000));
personList.add(new Person("多多", "男", 100, 9000));
personList.add(new Person("哈哈", "男", 36, 4000));
Person salaryMaxPerson = personList.stream().min(Comparator.comparingInt(Person::getSalary)).get();
System.out.println(salaryMaxPerson);
执行结果:
Person(name=张三, sex=男, age=17, salary=1000)