Java 1.8 stream,filter,map 练习

stream,filter,map 练习

public class FunctionTest {

    private List<Person> people = null;

    @Before
    public void before() {
        Person wu = new Person("小鱼", 18, "男");
        Person yu = new Person("kiet", 22, "男");
        people = Arrays.asList(wu, yu);
    }

    @Test
    public void Test() {
        // Filter过滤--性别为男
        List<Person> temp = people.stream().filter(person -> "男".equals(person.getSex()) && person.getAge() > 17).collect(toList());
        System.out.println(temp);

        // 取出名字 1
        List<String> names =people.stream().map(person -> person.getName()).collect(toList());
        System.out.println(names);

        // 取出年龄 2
        List<Integer> ages = people.stream().map(Person::getAge).collect(toList());
        System.out.println(ages);

        // 取出性别 3
        List<String> sexs = people.stream().map(person -> person.getSex()).collect(toList());
        System.out.println(sexs);


    }
}

你可能感兴趣的:(笔记而已)