Generic static method constrains types too much

后端 未结 3 1678
北海茫月
北海茫月 2021-01-25 05:28

In this answer I attempted to create a static utility method to make a List into a Map:

public static  Map toMa         


        
3条回答
  •  抹茶落季
    2021-01-25 06:11

    You could add a type parameter for the values of the map so they can be different from T:

    public static  Map toMapBy(List list,
            Function mapper) {
        return list.stream().collect(Collectors.toMap(mapper, Function.identity()));
    }
    

提交回复
热议问题