java8 list轉map

##List<T>轉Map<S,T>code

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

##List<T>轉Map<S,T>(過濾重複key)ci

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

##List<T>轉Map<S,S>get

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

##List<T>轉Map<S,List<T>>it

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

##docio

相關文章
相關標籤/搜索