l a s
l a s

Reputation: 3923

Java Map<String, String> to List<String> using Guava

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

Answers (4)

sasankad
sasankad

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

ruakh
ruakh

Reputation: 183602

You can write:

List<String> list = ImmutableList.copyOf(map.values());

Upvotes: 6

Reimeus
Reimeus

Reputation: 159874

Why not just do

List<String> list = new ArrayList<>(map.values());

Upvotes: 16

Chris M
Chris M

Reputation: 52

You can iterate yourselfe through the map and add each value to the list. Where is the problem?

Upvotes: -2

Related Questions