Reputation: 3923
I have a
java.util.Map<String, String>
and is there anyway in guava to convert that to a
List<String>
with only the values from the map?
Upvotes: 2
Views: 2415
Reputation: 3613
List<String> list = new ArrayList<String>(map.values());
EDIT
If you really need to use guava try it as follows
public List<String> mapToList(final Map<String, String> input){
return Lists.newArrayList(
Iterables.transform(
input.entrySet(), new Function<Map.Entry<String, String>, String>(){
@Override
public String apply(final Map.Entry<String, String> input){
return input.getValue();
}
}));
}
Upvotes: 2
Reputation: 183602
You can write:
List<String> list = ImmutableList.copyOf(map.values());
Upvotes: 6
Reputation: 159874
Why not just do
List<String> list = new ArrayList<>(map.values());
Upvotes: 16
Reputation: 52
You can iterate yourselfe through the map and add each value to the list. Where is the problem?
Upvotes: -2