java 8 集合_Java8 实现stream将对象集合list中抽取属性集合转化为map或list

首先新建一个实体类Person

@Data

public class Person {

/** 编码 */

private String code;

/** 名字 */

private String name;

public Person(String code, String name) {

this.code = code;

this.name = name;

}

}

实例化三个对象放入list集合中

public static void main(String[] args) {

Person person1 = new Person("001", "张三");

Person person2 = new Person("002", "李四");

Person person3 = new Person("002", "王五");

List personList = new ArrayList<>();

personList.add(person1);

personList.add(person2);

personList.add(person3);

personList.forEach(t -> System.out.println(t.toString()));

}

输出结果为:

Person(code=001, name=张三)

Person(code=002, name=李四)

你可能感兴趣的:(java,8,集合)