Java8 list to map

  java

List<T > to Map<S,T >

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

List<T > to Map<S,T > (Filter duplicate key)

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

List<T > to Map<S,S >

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

List<T > to Map<S,List<T > >

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

doc