使用stram流从list集合中取出一个或两个属性值

 public static void main(String[] args) {

    List list=new ArrayList<>();
    list.add(new PersonDo("藏三",17,"男",167L,60L));
    list.add(new PersonDo("李肆",18,"未知",180L,100L));
    list.add(new PersonDo("王武",19,"女",172L,55L));


    List nameList = list.stream().map(PersonDo::getName).collect(Collectors.toList());
    System.out.println("只获取一个元素:————————");
    for (String name:nameList) {
        System.out.println(name);
    }
    System.out.println();

    Map nameAndAgeList = list.stream().collect(Collectors.toMap(PersonDo::getName, PersonDo::getAge));
    System.out.println("获取两个元素:————————");
    for (Map.Entry map:nameAndAgeList.entrySet()) {
        System.out.println(map.getKey()+","+map.getValue());
    }
    System.out.println();

    List personDoList = list.stream().filter(man -> "王武".equals(man.getName())).collect(Collectors.toList());
    System.out.println("条件过滤:————————");
    for (PersonDo p:personDoList) {
        System.out.println(p.toString());
    }
    System.out.println();

    Map nameAndAgeWithFileByName = list.stream().filter(man -> "藏三".equals(man.getName())).collect(Collectors.toMap(PersonDo::getName, PersonDo::getAge));
    System.out.println("条件过滤+部分属性:————————");
    for (Map.Entry map:nameAndAgeWithFileByName.entrySet()) {
        System.out.println(map.getKey()+","+map.getValue());
    }
}

使用stram流从list集合中取出一个或两个属性值_第1张图片

你可能感兴趣的:(java,java,前端,后端)