How to convert List <String> to Map <String, String> with google collections?

I have a list with strings, and I have functions to generate a value for each key in the list, and I want to create a map using the method. is there such a feature in google collections?

+44
java list guava map
Feb 15 '11 at 12:48
source share
5 answers

Starting on 7/26/2012, the Guava wizard 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 ) allow you to view Set plus a Function as Map , where the value for each key in the set is the result of applying a function to it the key. The result is a view, so it does not copy the set of input data, and the Map result will change as the set does, and vice versa.

Maps.toMap(Iterable<K>, Function<? super K, V>) accepts Iterable and eagerly converts it to ImmutableMap , where the individual elements of the iterable are the keys, and the values ​​are the results of applying the function to each key.

+19
Jul 27 '12 at 1:25
source share

Use Maps.uniqueIndex (Iterable, Function) :

Returns an immutable map, for which Map.values ​​() are the given elements in the given order, and each key is the product of calling the supplied function to the 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; }}); 
+82
Feb 15 2018-11-15T00:
source share

EDIT: It is entirely possible that Sean and I misunderstood this question.

If the source list is for keys, then it sounds like you can just use a compute map using MapMaker.makeComputingMap and ignore the input list to start with. EDIT: As noted in the comments, this is now deprecated and removed in Guava 15.0. Take a look at CacheBuilder instead.

On the other hand, this also does not give you a card that returns null if you ask him for a value corresponding to a key that was not at the top of the list. It also will not give you. In other words, this may not be acceptable, but it is worth considering, depending on what you are trying to do with it. :)

I will leave this answer here if you do not comment that none of these methods is useful here, in which case I will delete it.




Original answer

Using Guava , you can do this quite easily with Maps.uniqueIndex :

 Map<String, String> map = Maps.uniqueIndex(list, keyProjection); 

(I mentioned Guava specifically, unlike Google collections, since I did not check if the old Google Maps.uniqueIndex collections repository Maps.uniqueIndex .)

+15
Feb 15 '11 at 12:51
source share

Either I misunderstood you, or other posters. I understand that you want your list to be the map key, and Maps.uniqueIndex() creates the keys for matching with your values ​​(which is exactly the opposite).

Anyway, there is an open Guava problem that asks for the exact functions you are requesting, and I also implemented such a solution for the previous question.

+13
Feb 15 '11 at 15:39
source share

Using Guava + Lamba

  Map<String, YourCustomClass> map = Maps.uniqueIndex(YourList, YourCustomClass -> YourCustomClass.getKey()); 
+6
Oct 29 '14 at 9:19
source share



All Articles