测试类

  • Person.java

后面的测试都是以Person对象的集合处理展开

@Data
@AllArgsConstructor
@ToString
class Person {
    // 姓名
    String name;
    // 性别
    String sex;
    // 年纪
    Integer age;
    // 工资
    Integer salary;
}

循环 forEach

  • List循
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循环
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)]

结果收集 collect

  • 收集集合流中的所有人的姓名,拼接返回字符串
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);

执行结果:

张三,李四,王五,张三,多多,哈哈

映射 map

映射原始集合,结合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);

执行结果:

[张三, 李四, 王五, 赵六, 多多]

过滤 filter

找出列表中满足条件元素

  • 找出年龄大于30岁的人
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)]

分组 Collectors.goupingBy

根据对象中的指定名称进行分组,返回一个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}

排序 sorted

  • 根据年龄顺序排序
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)]

去重 dictinct

  • 找出所有不重复的年龄
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]

求和 sum

  • 所有人的工资总和
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);

执行结果:

29000

平均值 average

  • 找出36岁年龄人群的平均工资
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

最大值 max

  • 找出工资最高的人
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)

最小值 min

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)

颜辉
1 声望0 粉丝