java8 list转map

List<T>转Map<S,T>

Map<String,DemoEntity> map = stats.stream().collect(Collectors.toMap(DemoEntity::getKey,
                c -> c));
                          

List<T>转Map<S,T>(过滤重复key)

Map<String,DemoEntity> result = items.stream().collect(Collectors.toMap(DemoEntity::getKey,
                c -> c,(e1,e2) -> e1));  

List<T>转Map<S,S>

Map<String,String> map = stats.stream().collect(Collectors.toMap(DemoEntity::getKey,
                DemoEntity::getStringValue));    

List<T>转Map<S,List<T>>

Map<String,List<DemoEntity>> map = vars.stream().collect(Collectors.groupingBy(DemoEntity::getKey));

doc

评论

暂无评论