How to transform List<String> to Map<String,String> with Google collections?

JavaListDictionaryGuava

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

Use 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.(from javadoc)

Example:

Map<String,String> mappedRoles = Maps.uniqueIndex(yourList, new Function<String,String>() {
  public String apply(String from) {
    // do stuff here
    return result;
  }});

Solution 2 - Java

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.

Solution 3 - Java

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. EDIT: As noted in comments, this is now deprecated and deleted in Guava 15.0. Have a look at CacheBuilder instead.

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. 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:

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.)

Solution 4 - Java

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).

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.

Solution 5 - Java

Using Guava + Lambda

 Map<String, YourCustomClass> map = Maps.uniqueIndex(YourList, YourCustomClass -> YourCustomClass.getKey());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoshaiView Question on Stackoverflow
Solution 1 - JavadogbaneView Answer on Stackoverflow
Solution 2 - JavaColinDView Answer on Stackoverflow
Solution 3 - JavaJon SkeetView Answer on Stackoverflow
Solution 4 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 5 - JavaCarlosView Answer on Stackoverflow