[英]How to transform List<String> to Map<String,String> with Google collections?
I have a list of strings and I have a function to generate a value for each key in the list.我有一个字符串列表,我有一个函数可以为列表中的每个键生成一个值。
I want to create a map using this function.我想使用此功能创建地图。 Can I do this with Google collections?我可以使用 Google 集合执行此操作吗?
Use Maps.uniqueIndex(Iterable, Function) :使用Maps.uniqueIndex(Iterable, Function) :
Returns an immutable map for which the Map.values() are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.返回一个不可变映射,其中 Map.values() 是给定顺序中的给定元素,每个键是对其相应值调用提供的函数的产物。 (from javadoc) (来自 javadoc)
Example:例子:
Map<String,String> mappedRoles = Maps.uniqueIndex(yourList, new Function<String,String>() {
public String apply(String from) {
// do stuff here
return result;
}});
As of 7/26/2012, Guava master contains two new ways to do this.截至 2012 年 7 月 26 日,番石榴大师包含两种新方法来做到这一点。 They should be in release 14.0.它们应该在 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. Maps.asMap(Set<K>, Function<? super K, V>)
(以及SortedSet
和NavigableSet
两个重载)允许您将Set
和Function
作为Map
查看,其中Set
中每个键的值是将该函数应用于该键的结果。 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.结果是一个视图,因此它不会复制输入集,并且Map
结果将随着集的变化而变化,反之亦然。
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. Maps.toMap(Iterable<K>, Function<? super K, V>)
接受一个Iterable
并急切地将它转换为一个ImmutableMap
,其中 iterable 的不同元素是键,值是将函数应用于每个的结果钥匙。
EDIT: It's entirely possible that Sean's right and I misunderstood the question.编辑:肖恩完全有可能是对的,我误解了这个问题。
If the original list is meant to be keys , then it sounds like you might be able to just use a computing map, via MapMaker.makeComputingMap
, and ignore the input list to start with.如果原始列表是keys ,那么听起来您可能只能通过MapMaker.makeComputingMap
使用计算地图,而忽略输入列表开始。 EDIT: As noted in comments, this is now deprecated and deleted in Guava 15.0.编辑:如评论中所述,现在已在 Guava 15.0 中弃用并删除。 Have a look at CacheBuilder
instead.来看看CacheBuilder
。
On the other hand, that also doesn't give you a map which will return null if you ask it for a value corresponding to a key which wasn't in the list to start with.另一方面,这也不会为您提供一个映射,如果您要求它提供与开始时不在列表中的键对应的值,则该映射将返回 null。 It also won't give you In other words, this may well not be appropriate, but it's worth consideration, depending on what you're trying to do with it.它也不会给你换而言之,这很可能是不合适的,但它是值得考虑的,这取决于你想用它做什么。 :) :)
I'll leave this answer here unless you comment that neither approach here is useful to you, in which case I'll delete it.我会把这个答案留在这里,除非你评论说这里的两种方法对你都没有用,在这种情况下我会删除它。
Original answer原答案
Using Guava you can do this pretty easily with Maps.uniqueIndex
:使用Guava,您可以使用Maps.uniqueIndex
轻松完成此Maps.uniqueIndex
:
Map<String, String> map = Maps.uniqueIndex(list, keyProjection);
(I mentioned Guava specifically as opposed to Google collections, as I haven't checked whether the older Google collections repository includes Maps.uniqueIndex
.) (我特别提到了 Guava,而不是 Google 集合,因为我还没有检查旧的 Google 集合存储库是否包含Maps.uniqueIndex
。)
Either I have misunderstood you or the other posters have.要么我误解了你,要么其他海报都误解了。 I understand that you want your list to be the map keys, while Maps.uniqueIndex()
creates keys to map to your values (which is quite the opposite).我知道您希望您的列表成为地图键,而Maps.uniqueIndex()
创建键以映射到您的值(这恰恰相反)。
Anyway, there is an open Guava issue that requests the exact functionality you are requesting, and I have also implemented such a solution for a previous question.无论如何,有一个开放的 Guava 问题需要您请求的确切功能,我也为之前的问题实施了这样的解决方案。
使用番石榴 + Lambda
Map<String, YourCustomClass> map = Maps.uniqueIndex(YourList, YourCustomClass -> YourCustomClass.getKey());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.