java8 List集合的排序,求和,取最大值,按照条件过滤

转载(https://www.cnblogs.com/c9999/p/10844654.html)

public class Java8Test {

public static void main(String[] args) {

    Person p1 = new Person("麻子", 31);
    Person p2 = new Person("李四", 20);
    Person p3 = new Person("王五", 26);

    List personList = new ArrayList();
    personList.add(p1);
    personList.add(p2);
    personList.add(p3);

    //java8遍历
    personList.forEach(p -> System.out.println(p.getAge()));
    //按照person的 age进行排序
    //方法一
    personList.sort((o1, o2) -> o1.getAge().compareTo(o2.getAge())); //正序
    personList.sort((o1, o2) -> o2.getAge().compareTo(o1.getAge())); //倒序
    //方法二
    personList.sort(Comparator.comparing(Person::getAge)); // 正序
    personList.sort(Comparator.comparing(Person::getAge).reversed()); // 倒序
    //遍历
    personList.forEach(p -> System.out.println(p.getAge()));

    System.out.println("========================================");

    //获取年龄最大的Person
    Person maxAgePerson = personList.stream().max(Comparator.comparing(Person::getAge)).get();
    System.out.println(maxAgePerson.getAge());

    System.out.println("========================================");

    //获取年龄最小的Person
    Person minAgePerson = personList.stream().min(Comparator.comparing(Person::getAge)).get();
    System.out.println(minAgePerson.getAge());

    //过滤出年龄是20的person,想过滤出什么条件的都可以
    List personList1 = personList.stream().filter(person -> person.getAge() == 20).collect(Collectors.toList());

    //统计出年龄等于20的个数
    long count = personList.stream().filter(person -> person.getAge() == 20).count();

    //获得年龄的平均值
    double asDouble = personList.stream().mapToInt(person -> person.getAge()).average().getAsDouble();

    //获得年龄的求和
    int sum = personList.stream().mapToInt(person -> person.getAge()).sum();

}

}

你可能感兴趣的:(java8 List集合的排序,求和,取最大值,按照条件过滤)