List转Map//方法一Map<Integer, GoodsVO> map = goodsListForWhale.stream().collect(Collectors.toMap(GoodsVO::getId, h -> h));//方法二Map<Integer, GoodsVO> map = goodsListForWhale.stream().collect(Collectors.groupingBy(GoodsVO::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
List转Map抽取对象的code作为key,name作为value转化为map集合。
private static HashMap<String, String> listToMap(List<Person> personList) { return (HashMap<String, String>)personList.stream() .filter(t -> t.getName()!=null) .collect(Collectors.toMap(Person::getCode,Person::getName,(k1,k2)->k2));}
filter() 方法作用是过滤掉名字为空的对象,当对象的名字为null时,会出现NPE空指针异常
(k1,k2)->k2 意思是遇到相同的key时取第二个值
(k1,k2)->k1 意思是遇到相同的key时取第一个值
List转Map>根据某个字段分组。
Map<String, List<Map>> lableGbType = list.stream().collect(Collectors.groupingBy(l -> (String) l.get("id")));
List根据对象的属性进行去重//对集合的结果进行去重//根据userid 去重List<User> list = userList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserId))) ,ArrayList::new));System.out.println(list);
32个Java Stream案例
//方法一Map<Integer, GoodsVO> map = goodsListForWhale.stream().collect(Collectors.toMap(GoodsVO::getId, h -> h));//方法二Map<Integer, GoodsVO> map = goodsListForWhale.stream().collect(Collectors.groupingBy(GoodsVO::getId,Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
List转Map抽取对象的code作为key,name作为value转化为map集合。
private static HashMap<String, String> listToMap(List<Person> personList) { return (HashMap<String, String>)personList.stream() .filter(t -> t.getName()!=null) .collect(Collectors.toMap(Person::getCode,Person::getName,(k1,k2)->k2));}
filter() 方法作用是过滤掉名字为空的对象,当对象的名字为null时,会出现NPE空指针异常
(k1,k2)->k2 意思是遇到相同的key时取第二个值
(k1,k2)->k1 意思是遇到相同的key时取第一个值
List转Map>根据某个字段分组。
Map<String, List<Map>> lableGbType = list.stream().collect(Collectors.groupingBy(l -> (String) l.get("id")));
List根据对象的属性进行去重//对集合的结果进行去重//根据userid 去重List<User> list = userList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserId))) ,ArrayList::new));System.out.println(list);
32个Java Stream案例
抽取对象的code作为key,name作为value转化为map集合。
private static HashMap<String, String> listToMap(List<Person> personList) {return (HashMap<String, String>)personList.stream().filter(t -> t.getName()!=null).collect(Collectors.toMap(Person::getCode,Person::getName,(k1,k2)->k2));}
filter() 方法作用是过滤掉名字为空的对象,当对象的名字为null时,会出现NPE空指针异常
(k1,k2)->k2 意思是遇到相同的key时取第二个值
(k1,k2)->k1 意思是遇到相同的key时取第一个值
List转Map>根据某个字段分组。
Map<String, List<Map>> lableGbType = list.stream().collect(Collectors.groupingBy(l -> (String) l.get("id")));
List根据对象的属性进行去重//对集合的结果进行去重//根据userid 去重List<User> list = userList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserId))) ,ArrayList::new));System.out.println(list);
32个Java Stream案例
根据某个字段分组。
Map<String, List<Map>> lableGbType =list.stream().collect(Collectors.groupingBy(l -> (String) l.get("id")));
List根据对象的属性进行去重//对集合的结果进行去重//根据userid 去重List<User> list = userList.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserId))) ,ArrayList::new));System.out.println(list);
32个Java Stream案例
//对集合的结果进行去重//根据userid 去重List<User> list = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserId))),ArrayList::new));System.out.println(list);
