List使用stream流转成map的几种方式
Posted 小小小米fly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List使用stream流转成map的几种方式相关的知识,希望对你有一定的参考价值。
List使用stream流转成map的几种方式
- 实体例子
- List<Object> 转成Map<String,Object>
- List<Object> 转成Map<String,String>
- List<Object> 转成Map<String,List<Object>>
- List<Object> 转成Map<String,List<String>>
- List<Map<String,Object>> 转成Map<String,Map<String,Object>>
- List<Map<String,Object>> 转成Map<String,Object>
- List<Map<String,String>> 转成Map<String,Map<String,String>>
- List<Map<String,String>> 转成Map<String,String>
实体例子
public class Person
private String name;
private String address;
public Person(String name, String address)
this.name = name;
this.address = address;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAddress()
return address;
public void setAddress(String address)
this.address = address;
List 转成Map<String,Object>
List<Person> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,Person> map = list.stream().collect(Collectors.toMap(Person::getName,each->each,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“光头强”:“address”:“森林第三个小屋”,“name”:“光头强”,“熊大”:“address”:“森林第一个小屋”,“name”:“熊大”,“熊二”:“address”:“森林第二个小屋”,“name”:“熊二”
List 转成Map<String,String>
List<Person> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,String> map = list.stream().collect(Collectors.toMap(Person::getName,Person::getAddress,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“光头强”:“森林第三个小屋”,“熊大”:“森林第一个小屋”,“熊二”:“森林第二个小屋”
List 转成Map<String,List>
方法一:
List<Person> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getName));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“光头强”:[“address”:“森林第三个小屋”,“name”:“光头强”],“熊大”:[“address”:“森林第一个小屋”,“name”:“熊大”,“address”:“森林第四个小屋”,“name”:“熊大”],“熊二”:[“address”:“森林第二个小屋”,“name”:“熊二”]
方法二:
List<Person> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,List<Person>> map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each),(value1, value2) ->
List<Person> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“光头强”:[“address”:“森林第三个小屋”,“name”:“光头强”],“熊大”:[“address”:“森林第一个小屋”,“name”:“熊大”,“address”:“森林第四个小屋”,“name”:“熊大”],“熊二”:[“address”:“森林第二个小屋”,“name”:“熊二”]
List 转成Map<String,List>
List<Person> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Person person4 = new Person("熊大","森林第四个小屋");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,List<String>> map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each.getAddress()),(value1, value2) ->
List<String> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“光头强”:[“森林第三个小屋”],“熊大”:[“森林第一个小屋”,“森林第四个小屋”],“熊二”:[“森林第二个小屋”]
List<Map<String,Object>> 转成Map<String,Map<String,Object>>
List<Map<String,Object>> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Map<String,Object> map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map<String,Object> map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Map<String,Object>> map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each,(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“1”:“person”:“address”:“森林第一个小屋”,“name”:“熊大”,“id”:“1”,“2”:“person”:“address”:“森林第二个小屋”,“name”:“熊二”,“id”:“2”,“3”:“person”:“address”:“森林第三个小屋”,“name”:“光头强”,“id”:“3”
List<Map<String,Object>> 转成Map<String,Object>
List<Map<String,Object>> list = new ArrayList<>();
Person person1 = new Person("熊大","森林第一个小屋");
Person person2 = new Person("熊二","森林第二个小屋");
Person person3 = new Person("光头强","森林第三个小屋");
Map<String,Object> map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map<String,Object> map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Object> map = list.stream().collect(Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each.get("person"),(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“1”:“address”:“森林第一个小屋”,“name”:“熊大”,“2”:“address”:“森林第二个小屋”,“name”:“熊二”,“3”:“address”:“森林第三个小屋”,“name”:“光头强”
List<Map<String,String>> 转成Map<String,Map<String,String>>
List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map1 = new HashMap<>();
map1.put("id","1");
map1.put("name","熊大");
map1.put("address","森林第一个小屋");
Map<String,String> map2 = new HashMap<>();
map2.put("id","2");
map2.put("name","熊二");
map2.put("address","森林第二个小屋");
Map<String,String> map3 = new HashMap<>();
map3.put("id","3");
map3.put("name","光头强");
map3.put("address","森林第三个小屋");
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Map<String,String>> map = list.stream().collect(Collectors.toMap(each->each.get("id"),each->each,(key1,key2)->key1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
“1”:“address”:“森林第一个小屋”,“name”:“熊大”,“id”:“1”,“2”:“address”:“森林第二个小屋”,“name”:“熊二”,“id”:“2”,“3”:“address”:“森林第三个小屋”,“name”:“光头强”,“id”:“3”
List<Map<String,String>> 转成Map<String,String>
List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map1 = new HashMap<>(Java之List和Map的几种遍历方式
/**
* list和map遍历
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class list_map遍历{
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("HAHAHAHA");
//第一种遍历方法使用foreach遍历List
for (String str : list) { //也可以改写for(int i=0;i<list.size();i++)这种形式
System.out.println(str);
}
//第二种遍历,把链表变为数组相关的内容进行遍历
String[] strArray=new String[list.size()];
list.toArray(strArray);
for(int i=0;i<strArray.length;i++) //这里也可以改写为 foreach(String str:strArray)这种形式
{
System.out.println(strArray[i]);
}
//第三种遍历 使用迭代器进行相关遍历
Iterator<String> ite=list.iterator();
while(ite.hasNext())//判断下一个元素之后有值
{
System.out.println(ite.next());
}
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
}
以上是关于List使用stream流转成map的几种方式的主要内容,如果未能解决你的问题,请参考以下文章
Java 将List<Object; 转换成 Map<String, Map<String,List<Object;;;的几种方法