当前位置: 代码网 > it编程>编程语言>Java > Java8 stream 提取 List 中元素的某一字段生成新的 List

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

2025年11月26日 Java 我要评论
目录 省流: 正文  一、需求: 二、解读: 另一种写法:方法引用 三、详细解释 map常用的搭配   省流: 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。

另一种写法:方法引用

.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());
 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com