如何使用Stream流将List转换为Map

如何使用Stream流将List转换为Map

以下程序用到的基础代码:

final static List<Student> studentList = new ArrayList<Student>();

/**
 * 初始化集合数据
 */
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 转化为Map
Map<String, Student> map = studentList.stream().collect(Collectors.toMap(Student::getId, each -> each, (value1, value2) -> value1));

List转化为Map
Map<String, String> map = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getAddress, (value1, value2) -> value1));

List转化为Map>
Map<Integer, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getAge));

List转化为Map>
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

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));
	}


	/**
	 * 初始化集合数据
	 */
	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>

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

	}

	/**
	 * 初始化集合数据
	 */
	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

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

	}

	/**
	 * 初始化集合数据
	 */
	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);
	}

你可能感兴趣的:(Java,Web,java,开发语言)