如何转换List< String>映射< String,String>与谷歌收藏? [英] how to transform List<String> to Map<String,String> with google collections?
问题描述
截至7/26/2012,Guava master包含两种新方法。他们应该在版本14.0。
Maps.asMap(Set< K>,Function<?super K,V>)
(和两个重载 SortedSet
和 NavigableSet
)允许您查看集
加一个函数
作为 Map
其中集合中每个键的值为将该功能应用于该键的结果。结果是一个视图,所以它不会复制输入集,并且 Map
结果将随设置而改变,反之亦然。
Maps.toMap(Iterable< K>,Function<?super K,V>)
取一个 Iterable
并将其转换为 ImmutableMap
,其中iterable的不同元素是键,值是将函数应用于每个键的结果。 p>
I have a list with strings, and I have a functions to generate value for each key in the list, and I want to create map using a method. is there such function in google collections?
As of 7/26/2012, Guava master contains two new ways to do this. They should be in release 14.0.
Maps.asMap(Set<K>, Function<? super K, V>)
(and two overloads for SortedSet
and NavigableSet
) allows you to view a Set
plus a Function
as a Map
where the value for each key in the set is the result of applying the function to that key. The result is a view, so it doesn't copy the input set and the Map
result will change as the set does and vice versa.
Maps.toMap(Iterable<K>, Function<? super K, V>)
takes an Iterable
and eagerly converts it to an ImmutableMap
where the distinct elements of the iterable are the keys and the values are the results of applying the function to each key.
这篇关于如何转换List< String>映射< String,String>与谷歌收藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!