如何转换List< String>映射< String,String>与谷歌收藏? [英] how to transform List<String> to Map<String,String> with google collections?

查看:135
本文介绍了如何转换List< String>映射< String,String>与谷歌收藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字符串的列表,我有一个函数为列表中的每个键生成值,我想使用一个方法创建地图。在Google收藏中有这样的功能吗?

解决方案

截至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&lt; String&gt;映射&lt; String,String&gt;与谷歌收藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆