java8 stream 流遍历 List分组Map

        @Test
	public void fun1() {
		    List> lsl = new ArrayList<>();
		
		Map map = new HashMap<>();
		map.put("id", "1");
		map.put("name", "zhangSan");
		lsl.add(map);
		
		Map map2 = new HashMap<>();
		map2.put("id", "2");
		map2.put("name", "lisi");
		lsl.add(map2);

		Map map3 = new HashMap<>();
		map3.put("id", "1");
		map3.put("name", "wangwu");
		
		lsl.add(map3);
		
		Map map4 = new HashMap<>();
		map4.put("id", "2");
		map4.put("name", "zhaoliu");
		
		lsl.add(map4);
		
		Map>> collect = lsl.stream().collect(Collectors.groupingBy(s));
		
		System.out.println(collect);
	}
	
	Function, String>  s = new Function, String>() {

		@Override
		public String apply(Map t) {
			Object object = t.get("id");
			String string = object.toString();
			return string;
		}
	};  

结果:

java8 stream 流遍历 List分组Map_第1张图片

使用Function 返回你需要分组的内容

你可能感兴趣的:(java)