Lambda的stream流操作进行集合的过滤、排序、统计

public static void main(String[] args) {
List students=new ArrayList<>();
students.add(new Student("李四",18));
students.add(new Student("王五",28));
students.add(new Student("张三",12));
students.add(new Student("麻子",38));
//过滤
students.stream().filter(student ->   student.getAge()>30).collect(Collectors.toList()).forEach(student -> System.out.println(student.getName()));
//选择元素重组新集合
students.stream().map(student -> student.getName()).collect(Collectors.toList()).forEach(student -> System.out.println(student));
//根据集合的元素进行集合的排序
students.stream().sorted(Comparator.comparing(student -> student.getAge())).collect(Collectors.toList()).forEach(student -> System.out.println(student.getAge()));
根据集合的元素进行统计
System.out.println(students.stream().mapToInt(sss->sss.getAge()).sum());
System.out.println(students.stream().mapToInt(sss->sss.getAge()).average());
System.out.println(students.stream().mapToInt(sss->sss.getAge()).min());
System.out.println(students.stream().map(sss->sss.getAge()>30).count());
System.out.println(students.stream().mapToInt(sss->sss.getAge()).max());
System.out.println(string1.stream().mapToInt(eee->Integer.valueOf(eee)).max().getAsInt());

}

static class Student{
       private String name;
       private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

Lambda的stream流操作进行集合的过滤、排序、统计_第1张图片

你可能感兴趣的:(java技术)