java8 list转map

List转Map

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

List转Map(过滤重复key)

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

List转Map

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

List转Map>

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

doc

  • Ignore duplicates when producing map using streams

你可能感兴趣的:(java)