Reputation: 17654
Using Guava, is it possible to convert a Map to a Set or List of objects containing the keys and values of the Map? E.g. given something like
class MyEntry {
public String key;
public String value;
}
Map<String,String> theMap = new Map<String,String>();
I am missing something like this in Guava / did not find it:
Set<MyEntry> myEntries = Maps.transform(theMap, transformFunction<<Map<String,String>, MyEntry>);
Obviously it's not hard to do this manually, still I am wondering if I missed something and Guava really doesn't support such a thing?
Thanks for any hint!
Upvotes: 1
Views: 3001
Reputation: 198123
The closest thing you can do with Guava would be something like
Collection<MyEntry> myEntries = Collections2.transform(map.entrySet(), function);
which is just a normal transformation on the entrySet
of the map
, and function
has the type Function<Map.Entry<String, String>, MyEntry>
.
Guava can't provide Sets.transform
for a variety of reasons -- it can't guarantee the function is injective, it doesn't have an inverse to the function, etc. If you need a Set
, then you're probably best off just doing the manual loop.
Upvotes: 4