How does one convert a HashMap to a List in Java?
Solution 1
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put (1, "Mark");
map.put (2, "Tarryn");
List<String> list = new ArrayList<String>(map.values());
for (String s : list) {
System.out.println(s);
}
Solution 2
Assuming you have:
HashMap<Key, Value> map; // Assigned or populated somehow.
For a list of values:
List<Value> values = new ArrayList<Value>(map.values());
For a list of keys:
List<Key> keys = new ArrayList<Key>(map.keySet());
Note that the order of the keys and values will be unreliable with a HashMap; use a LinkedHashMap if you need to preserve one-to-one correspondence of key and value positions in their respective lists.
Solution 3
Solution using Java 8 and Stream Api:
private static <K, V> List<V> createListFromMapEntries (Map<K, V> map){
return map.values().stream().collect(Collectors.toList());
}
Usage:
public static void main (String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
List<String> result = createListFromMapEntries(map);
result.forEach(System.out :: println);
}
Solution 4
Collection Interface has 3 views
- keySet
- values
- entrySet
Other have answered to to convert Hashmap into two lists of key and value. Its perfectly correct
My addition: How to convert "key-value pair" (aka entrySet)into list.
Map m=new HashMap();
m.put(3, "dev2");
m.put(4, "dev3");
List<Entry> entryList = new ArrayList<Entry>(m.entrySet());
for (Entry s : entryList) {
System.out.println(s);
}
ArrayList has this constructor.
Solution 5
Basically you should not mess the question with answer, because it is confusing.
Then you could specify what convert mean and pick one of this solution
List<Integer> keyList = Collections.list(Collections.enumeration(map.keySet()));
List<String> valueList = Collections.list(Collections.enumeration(map.values()));
sparkyspider
I have a passion for writing complex code in such a simple and understandable (followable) manner that a kid can understand and maintain it.
Updated on July 08, 2022Comments
-
sparkyspider almost 2 years
In Java, how does one get the values of a
HashMap
returned as aList
? -
sparkyspider about 13 yearsBasically, the .values() method of the HashMap class returns a Collection of the values. You can also use .keys() for that matter. The ArrayList() class accepts a Collection as one of its constructors. Hence, you create a new ArrayList from a Collection of the HashMap values. I have no idea what the performance is like, but for me the simplicity is worth it!
-
Nishant about 13 yearsif you just want to print or get objects - you do not need to use List.
-
Joachim Sauer about 13 yearsA
Map
has akeySet()
method. Using that would be simpler than what you posted for thekeyList
. -
Damian Leszczyński - Vash about 13 years@Joachim, that is the correct method name that im thankful for. But if you want to convert the Set into list this is the way.
-
sparkyspider about 13 yearsJust putting that there to clarify.
-
Joachim Sauer about 13 yearsqVash: why? you can simply use
new ArrayList(map.keySet())
which is a more direct route and avoid creating anEnumeration
. And I'm almost certain that it will perform better, because theArrayList
will be created with the correct size from the very beginning. -
sparkyspider about 13 yearsWould this return a reference to the original values, and not a new set of objects?
-
Joachim Sauer about 13 years@Mark: the end effect is the same as what you posted: You'll have a new
ArrayList
referencing the same object that are also referenced by theMap
. -
Damian Leszczyński - Vash about 13 years@Mark, if you refer to the values from map will do, but this solution seam to work slower than
new ArrayList(Collection<T> col)
because there the table are copied to array at once using Arrays.copy function. -
bcorso over 8 years+1 for noting proper ordering should use
LinkedHashMap
<=>List
. Also, it's probably good practice for the unordered conversions to useHashMap
<=>Set
to indicate that it's unordered. -
MAbraham1 over 8 yearsDue to its simplicity, this appears to be a better solution than the answer chosen.