当前位置:   article > 正文

Java8 stream 提取 List 中元素的某一字段生成新的 List_stream抽取字段作为list

stream抽取字段作为list

目录

省流:

正文 

一、需求:

二、解读:

另一种写法:方法引用

三、详细解释

map常用的搭配


 

省流:

  1. List<String> nameList  = list.stream().map(o -> o.getName()).collect(Collectors.toList());
  2. //或者
  3. 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提取出来

  1. List<Person> personList = new ArrayList<>();
  2. List<String> nameList =
  3. personList.stream().map(o -> o.getName()).collect(Collectors.toList());
  4. //另一种写法:
  5. 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() 去重

  1. //假如list里装的是map
  2. List<Map<String,String>> list3 = new ArrayList<>();
  3. Map<String,String> map10 = new HashMap<>();
  4. map10.put("name","john");
  5. Map<String,String> map11 = new HashMap<>();
  6. map11.put("name","john");
  7. Map<String,String> map12 = new HashMap<>();
  8. map12.put("name","tom");
  9. list3.add(map11);list3.add(map10);list3.add(map12);
  10. List<String> list = list3.stream().map(o->o.get("name")).distinct().collect(Collectors.toList());
  11. //打印出来:[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());
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/323272
推荐阅读
相关标签
  

闽ICP备14008679号