从List对象集合中取出一个或者两个属性集合(含条件过滤)

注意:仅支持JDK1.8及其以上环境

 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());
    }
    // 防止map中的key重复异常,可以设置表达式覆盖取,v2后面覆盖前面,取v1前面覆盖后面
    Map collect = list.stream().collect(Collectors.toMap(PersonDO::getId, PersonDO::getName, (v1, v2) -> v2));
    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());
    }
}

输出结果:
从List对象集合中取出一个或者两个属性集合(含条件过滤)_第1张图片

.
Person实体类

import java.io.Serializable;
public class PersonDo implements Serializable {
    private static final long serialVersionUID = 999532693980436459L;

    /**
     * 姓名
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 性别
     */
    private String sex;
    /**
     * 身高 单位:cm
     */
    private Long height;
    /**
     * 体重 单位:kg
     */
    private Long weight;

    public PersonDo() {
    }

    public PersonDo(String name, Integer age, String sex, Long height, Long weight) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.height = height;
        this.weight = weight;
    }

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Long getHeight() {
        return height;
    }

    public void setHeight(Long height) {
        this.height = height;
    }

    public Long getWeight() {
        return weight;
    }

    public void setWeight(Long weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "PersonDo{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                '}';
    }
}

建议:最好去看一下Lambda 表达式这篇写的不错https://www.cnblogs.com/qdhxhz/p/9393724.html

你可能感兴趣的:(java开发,java,lambda,stream)