Java8 stream 提取 List 中元素的某一字段生成新的 List

目录

省流:

正文 

一、需求:

二、解读:

另一种写法:方法引用

三、详细解释

map常用的搭配


 

省流:

List<String> nameList  = list.stream().map(o -> o.getName()).collect(Collectors.toList());
//或者
List<String> nameList  = list.stream().map(Person::getName).collect(Collectors.toList());

正文 

一、需求:

想要将List中实体的某个字段的值提取出来

List<String> newList = objectList.stream().map(Obj::getVal).collect(Collectors.toList());

将object换成你的实体类即可。

例如:想要将List中person的name提取出来

List<Person> personList = new ArrayList<>();
List<String> nameList = 
personList.stream().map(o -> o.getName()).collect(Collectors.toList());

//另一种写法:
personList.stream().map(Person::getName).collect(Collectors.toList());

.map()方法通常用于提取某个字段。

二、解读:

1、.map(o -> o.getXXX())例如提取姓名,o.getName()o代表list里的元素,一个一个遍历过去,可以理解为 for(Person o : list) 的o

2、.map() 直接跟在 .stream() 后使用。list.stream().map(o->o.getName())

3、提取完之后需要生成一个新的list,所以需要.collect(Collectors.toList())这是一个固定用法。

4、将两个拼起来,list.stream().map(o->o.getName()).collect(Collectors.toList()),就得到一个name的list。

List<String> nameList = list.stream().map(o->o.getName()).collect(Collectors.toList())

另一种写法:方法引用

.map(o->o.getName())经常会看到另一种写法:.map(xxx::getName),xxx是类名,例如:你的类叫Person,那就.map(Person::getName)这种类名+方法名,中间用两个冒号隔开的写法,就叫方法引用

三、详细解释

map:通过一个 Function 把一个元素类型为 T的流转换成元素类型为 R的流。

map属于中间操作,即操作完Stream,返回Stream,依然可以继续执行其他操作,允许链式串接。注意:流上的操作不会改变数据源。

map常用的搭配

1、distinct() 去重

//假如list里装的是map
        List<Map<String,String>> list3 = new ArrayList<>();
        Map<String,String> map10 = new HashMap<>();
        map10.put("name","john");
        Map<String,String> map11 = new HashMap<>();
        map11.put("name","john");
        Map<String,String> map12 = new HashMap<>();
        map12.put("name","tom");
        list3.add(map11);list3.add(map10);list3.add(map12);

List<String> list = list3.stream().map(o->o.get("name")).distinct().collect(Collectors.toList());

//打印出来:[john,tom]

2、forEach()

list.stream().map(Person::getName).forEach(o->list2.add(map.get(o)));

3、flatMap()

List<String> mapList = list.stream().flatMap(Arrays::stream).collect(Collectors.toList());

======================分割线========================

文章到这里已经结束了,以下是紫薯布丁

List<String> nameList  = list.stream().map(Person::getName).collect(Collectors.toList());

List<Object> newList = objectList.stream().map(Object::getVar).collect(Collectors.toList());
 

List<Person> personList = new ArrayList<>();
List<String> nameList = 
personList.stream().map(o -> o.getName()).collect(Collectors.toList());

//另一种写法:
personList.stream().map(Person::getName).collect(Collectors.toList());

//假如list里装的是map
        List<Map<String,String>> list3 = new ArrayList<>();
        Map<String,String> map10 = new HashMap<>();
        map10.put("name","john");
        Map<String,String> map11 = new HashMap<>();
        map11.put("name","john");
        Map<String,String> map12 = new HashMap<>();
        map12.put("name","tom");
        list3.add(map11);list3.add(map10);list3.add(map12);

List<String> list = list3.stream().map(o->o.get("name")).distinct().collect(Collectors.toList());

//打印出来:[john,tom]

list.stream().map(Person::getName).forEach(o->list2.add(map.get(o)));

List<String> mapList = list.stream().flatMap(Arrays::stream).collect(Collectors.toList());
 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

globalcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值