java8中如何将泛型为string的集合使用tomap转换位map

如果集合中的对象是string那么就不能使用funtion.indentity,针对string类型的集合就不能使用当前对象作为value吗

如有帮助,请采纳呀!!!

 

 

 List<String> demo = new ArrayList<>();
        demo.add("1");
        demo.add("2");
        Map<String, String> collect = demo.stream().collect(Collectors.toMap(key -> key, value -> value));
        System.out.println(collect);

 

你这个转为map后key是什么,value是什么

Map<String, ContractResp> cityMap = cityCodes.stream().collect(Collectors.toMap(key->key, o->contractResp)); key是cityCodes集合里面的string,value则是一个bo对象