List对象根据某个属性转成List<String)
转换
List<XrwDepart> JSRY =xrwDepartService.list(new QueryWrapper<XrwDepart>().eq("depart_name","减少人员"));
List<String> collect = JSRY.stream().map(XrwDepart::getRelationCode).collect(Collectors.toList());
前后端数据交互转换
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
-
注解@JsonFormat主要是后台到前台的时间格式的转换
-
注解@DataFormAT主要是前后到后台的时间格式的转换
解决Java
Collectors.toMap
方法 value为null不支持的问题
- 在需要把List转换为Map时,通常会使用到Collectors.toMap 方法来转换,但如果转换后的Map的Value有null 则执行时会抛出NullPointerException 异常。
- 演示代码:
static class Data{
int key;
String value;
public Data(int key, String value) {
this.key = key;
this.value = value;
}
}
@Test
public void test_toMap(){
List<Data> dataList = List.of(
new Data(1,"A"),
new Data(2,"B"),
new Data(3,"C"),
new Data(4,null),
new Data(5,null)
);
Map<Integer,String> dataMap = dataList.stream().collect(Collectors.toMap(item->item.key,item->item.value));
- Collectors.toMap 有实际有两个实现
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
return new CollectorImpl<>(HashMap::new,
uniqKeysMapAccumulator(keyMapper, valueMapper),
uniqKeysMapMerger(),
CH_ID);
}
public static <T, K, U, M extends Map<K, U>>
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory) {
BiConsumer<M, T> accumulator
= (map, element) -> map.merge(keyMapper.apply(element),
valueMapper.apply(element), mergeFunction);
return new CollectorImpl<>(mapFactory, accumulator, mapMerger(mergeFunction), CH_ID);
}
-
其中第一个实现,会在uniqKeysMapAccumulator(keyMapper, valueMapper)方法中通过Objects.requireNonNull(valueMapper.apply(element)) 控制value不能为null。
-
而第二个实现,最终会会调用Map.merge方法来合并value,这个方法里也会通过Objects.requireNonNull(value);来控制value不能为null。
解决方案
- 因此Collectors.toMap 方法是无法解决value为null 抛出异常的,但我们实现list转map的需求还是存在的,那么对于这类需求,可以选择另一种方式实现,就是用Collectors.groupingBy。
实现方案一
- 默认的Collectors.groupingBy 返回的是Map<Key,List
> 形式的结果,但我们需要返回的Map<Key,Value>形式的结果。因此需要对需要做额外的一些处理。
@Test
public void test_groupByMap(){
List<Data> dataList = List.of(
new Data(1,"A"),
new Data(2,"B"),
new Data(3,"C"),
new Data(4,null),
new Data(5,null)
);
Map<Integer,String> dataMap = dataList.stream()
.collect(Collectors.groupingBy(item->item.key,
HashMap::new,
Collectors.reducing(null,item->item.value,(v1,v2)->v2)
)
);
}
- 原理就是利用额外的Collectors.reducing来做收集处理,将最终的List
转换成 Value。
实现方案二
public void test_toMap4(){
List<Data> dataList = List.of(
new Data(1,"A"),
new Data(2,"B"),
new Data(3,"C"),
new Data(4,null),
new Data(5,null)
);
Map<Integer,String> dataMap = dataList.stream()
.collect(HashMap::new,(map,item)->map.put(item.key,item.value),(map1,map2)->map1.putAll(map2)
);
}
- 当然(map1,map2)->map1.putAll(map2) 也可以简化成HashMap::putAll