赞
踩
目录
- 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,依然可以继续执行其他操作,允许链式串接。注意:流上的操作不会改变数据源。
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());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。