如何使用Stream流將List轉(zhuǎn)換為Map
以下程序用到的基礎(chǔ)代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-767593.html
final static List<Student> studentList = new ArrayList<Student>();
/**
* 初始化集合數(shù)據(jù)
*/
static {
Student stu1 = new Student("0001", "張三", 12, "江蘇南京");
Student stu2 = new Student("0002", "李四", 14, "江蘇無錫");
Student stu3 = new Student("0003", "王二", 11, "浙江臺州");
Student stu4 = new Student("0004", "李五", 12, "浙江溫州");
studentList.add(stu1);
studentList.add(stu2);
studentList.add(stu3);
studentList.add(stu4);
}
List<Object> 轉(zhuǎn)化為Map<String,Object>
Map<String, Student> map = studentList.stream().collect(Collectors.toMap(Student::getId, each -> each, (value1, value2) -> value1));
List<Object>轉(zhuǎn)化為Map<String,String>
Map<String, String> map = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getAddress, (value1, value2) -> value1));
List<Object>轉(zhuǎn)化為Map<String,List<Object>>
Map<Integer, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
List<Object>轉(zhuǎn)化為Map<String,List<String>>
Map<String, List<String>> map3 = studentList.stream().collect(Collectors.toMap(Student::getId, each -> Collections.singletonList(each.getName()), (value1, value2) -> {
List<String> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
}));
List<Map<String,Object>> 轉(zhuǎn)化為Map<String,Object>
final static List<Map<String, Object>> mapStudentList = new ArrayList<>();
public static void main(String[] args) {
Map<String, Object> map4 = mapStudentList.stream().collect(Collectors.toMap(each -> Objects.toString(each.get("id"), ""), each -> each.get("student"), (key1, key2) -> key1));
}
/**
* 初始化集合數(shù)據(jù)
*/
static {
Student stu1 = new Student("0001", "張三", 12, "江蘇南京");
Student stu2 = new Student("0002", "李四", 14, "江蘇無錫");
Student stu3 = new Student("0003", "王二", 11, "浙江臺州");
Student stu4 = new Student("0004", "李五", 12, "浙江溫州");
Map<String, Object> map1 = new HashMap<>();
map1.put("id", "0001");
map1.put("student", stu1);
Map<String, Object> map2 = new HashMap<>();
map2.put("id", "0002");
map2.put("student", stu2);
Map<String, Object> map3 = new HashMap<>();
map3.put("id", "0003");
map3.put("student", stu3);
Map<String, Object> map4 = new HashMap<>();
map4.put("id", "0004");
map4.put("student", stu4);
mapStudentList.add(map1);
mapStudentList.add(map2);
mapStudentList.add(map3);
mapStudentList.add(map4);
}
List<Map<String,String>> 轉(zhuǎn)化為Map<String,Map<String,String>>
final static List<Map<String, String>> listMapList = new ArrayList<>();
public static void main(String[] args) {
Map<String, Map<String, String>> map5 = listMapList.stream().collect(Collectors.toMap(each -> each.get("id"), each -> each, (key1, key2) -> key1));
System.out.println("map5 = " + map5);
}
/**
* 初始化集合數(shù)據(jù)
*/
static {
Map<String, String> map1 = new HashMap<>();
map1.put("id", "0001");
map1.put("name", "張三");
map1.put("age", "12");
map1.put("address", "江蘇南京");
Map<String, String> map2 = new HashMap<>();
map2.put("id", "0002");
map2.put("name", "李四");
map2.put("age", "14");
map2.put("address", "江蘇無錫");
Map<String, String> map3 = new HashMap<>();
map3.put("id", "0003");
map3.put("name", "王二");
map3.put("age", "11");
map3.put("address", "浙江臺州");
Map<String, String> map4 = new HashMap<>();
map4.put("id", "0004");
map4.put("name", "李五");
map4.put("age", "12");
map4.put("address", "浙江溫州");
listMapList.add(map1);
listMapList.add(map2);
listMapList.add(map3);
listMapList.add(map4);
}
List<Map<String,String>> 轉(zhuǎn)化為Map<String,String>
final static List<Map<String, String>> listmapstringlist = new ArrayList<>();
public static void main(String[] args) {
Map<String, String> map6 = listmapstringlist.stream().collect(Collectors.toMap(each -> each.get("id"), each -> each.get("name"), (key1, key2) -> key1));
}
/**
* 初始化集合數(shù)據(jù)
*/
static {
Map<String, String> map1 = new HashMap<>();
map1.put("id", "0001");
map1.put("name", "張三");
map1.put("age", "12");
map1.put("address", "江蘇南京");
Map<String, String> map2 = new HashMap<>();
map2.put("id", "0002");
map2.put("name", "李四");
map2.put("age", "14");
map2.put("address", "江蘇無錫");
Map<String, String> map3 = new HashMap<>();
map3.put("id", "0003");
map3.put("name", "王二");
map3.put("age", "11");
map3.put("address", "浙江臺州");
Map<String, String> map4 = new HashMap<>();
map4.put("id", "0004");
map4.put("name", "李五");
map4.put("age", "12");
map4.put("address", "浙江溫州");
listmapstringlist.add(map1);
listmapstringlist.add(map2);
listmapstringlist.add(map3);
listmapstringlist.add(map4);
}
文章來源:http://www.zghlxwxcb.cn/news/detail-767593.html
到了這里,關(guān)于如何使用Stream流將List轉(zhuǎn)換為Map的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!