List<Person> list = new ArrayList<>();
Person p1 = new Person("张三", 20, "江西");
Person p2 = new Person("李四", 22, "北京");
Person p3 = new Person("赵六", 26, "广东");
list.add(p1);
list.add(p2);
list.add(p3);
String str = list.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println(str);//张三,李四,赵六
//转list集合
List<String> list2 = list.stream().map(Person::getName).collect(Collectors.toList());
/**
* 1、正常遍历
* 张三
* 李四
* 赵六
*/
list2.forEach(s -> System.out.println(s.toString()));
//2、条件遍历
list2.forEach(c->{
if(c.equals("张三")){
System.out.println(c);
}
});
//set集合
Set<String> set = list.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
Java8 使用 stream().map().collect提取对象的某一列值
最新推荐文章于 2024-07-30 12:55:42 发布