Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting List<Map.Entry<Key, Value>> to List<Key> in Java

Is there a convenient way for such conversion other than a for loop such as

List<Map.Entry<String, Integer>> entryList = new List<>(//initialization);
List<String>> keyList = new List<>(entryList.size());
for (Map.Entry<String, Integer> e : entryList) {
    keyList.add(e.getKey());
}

I would like the order to be preserved.

like image 519
Jenna Kwon Avatar asked Jan 13 '16 21:01

Jenna Kwon


People also ask

How do you convert a map key to a List?

We can convert Map keys to a List of Values by passing a collection of map values generated by map. values() method to ArrayList constructor parameter.

How do I change the key-value of a map in Java?

The replace(K key, V value) method of Map interface, implemented by HashMap class is used to replace the value of the specified key only if the key is previously mapped with some value. Parameters: This method accepts two parameters: key: which is the key of the element whose value has to be replaced.

Can we use List as key in map in Java?

If you want to use an ArrayList as a map key, you will need to override equals() and hashcode() in order to make two arraylists with the same content in the same order return true on a call to equals() and return the same hashcode on a call to hashcode() .

How do I get the value of a map for a specific key?

util. HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.


2 Answers

Use java 8 streams to convert this:

List<Map.Entry<String, ?>> entryList = new List<>(//initialization);
List<String> stringList = entryList.stream().map(Entry::getKey).collect(Collectors.toList());

This makes a stream of the entries, then uses the map method to convert them to strings, then collects it to a list using Collectors.toList().

Alternatively, this method can be changed in a helper function if you need it more times like this:

public static <K> List<K> getKeys(List<Map.Entry<K,?>> entryList) {
    return entryList.stream().map(Entry::getKey).collect(Collectors.toList());
}

public static <V> List<V> getValues(List<Map.Entry<?,V>> entryList) {
    return entryList.stream().map(Entry::getValue).collect(Collectors.toList());
}

While the above code works, you can also get a List<K> from a map by doing new ArrayList<>(map.keySet()), with this having the advantage than you don't need to convert the entryset to a list, before converted to a stream, and then back a list again.

like image 106
Ferrybig Avatar answered Nov 14 '22 01:11

Ferrybig


If you do not really need to make a copy of the list you can implement a wrapper arround the list like this, with the adicional bonus that changes made to the entryList are automatically reflected in the stringList. Bear in mind that this simple wrapper is read only.

List<Map.Entry<String, ?>> entryList = new List<>(//initialization);
List<String> stringList = new AbstractList<String>() {
    List<Map.Entry<String, Integer>> internal = entryList;

    public String get(int index) {
        return internal.get(index).getKey();
    }

    public int size() {
        return internal.size();
    }
};
like image 42
Antonio Raposo Avatar answered Nov 14 '22 01:11

Antonio Raposo