我在guava中找不到将Collection(或Iterator/Iterable)转换为Map的方法,如下所示(为清楚起见省略了通配符):publicstaticMapcollectionSplitter(Collectionsource,FunctionkProducer,FunctionvProducer){Mapmap=Maps.newHashMap();for(Tt:source){map.put(kProducer.apply(t),vProducer.apply(t));}returnmap;}是否有任何现有方法可以做到这一点?如果T是字符串,我能找到的最接近的是Spli
我有一个字符串列表,我有一个函数可以为列表中的每个键生成一个值。我想用这个函数创建一个map。我可以使用GoogleCollections吗? 最佳答案 使用Maps.uniqueIndex(Iterable,Function):ReturnsanimmutablemapforwhichtheMap.values()arethegivenelementsinthegivenorder,andeachkeyistheproductofinvokingasuppliedfunctiononitscorrespondingvalue.(f
我有一个字符串列表,我有一个函数可以为列表中的每个键生成一个值。我想用这个函数创建一个map。我可以使用GoogleCollections吗? 最佳答案 使用Maps.uniqueIndex(Iterable,Function):ReturnsanimmutablemapforwhichtheMap.values()arethegivenelementsinthegivenorder,andeachkeyistheproductofinvokingasuppliedfunctiononitscorrespondingvalue.(f