5 List排序--sort

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/89333259
package com.java8.list;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gp6
 * @date 2019-04-15
 */
public class TestList {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<Person>() {{
            add(new Person(1, "小明", 11, 1));
            add(new Person(2, "小红", 12, 0));
            add(new Person(3, "小兰", 13, 0));
            add(new Person(4, "小强", 14, 0));
            add(new Person(5, "小咪", 10, 1));
            add(new Person(6, "小子", 11, 1));
            add(new Person(7, "小虾", 12, 1));
            add(new Person(7, "小哈", 12, 1));
        }};

		// 先取出性别为0的列表,在根据年龄排序
        List<Person> personListSort = personList.stream()
                .filter(person -> person.getSex().equals(0)).sorted(Comparator.comparingInt(Person::getAge))
                .collect(Collectors.toList());

        // [Person(id=2, name=小红, age=12, sex=0), Person(id=3, name=小兰, age=13, sex=0), Person(id=4, name=小强, age=14, sex=0)]
        System.out.println(personListSort);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/89333259