How does one convert a HashMap to a List in Java?

158,737

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()));
Share:
158,737
sparkyspider
Author by

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, 2022

Comments

  • sparkyspider
    sparkyspider almost 2 years

    In Java, how does one get the values of a HashMap returned as a List?

  • sparkyspider
    sparkyspider about 13 years
    Basically, 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
    Nishant about 13 years
    if you just want to print or get objects - you do not need to use List.
  • Joachim Sauer
    Joachim Sauer about 13 years
    A Map has a keySet() method. Using that would be simpler than what you posted for the keyList.
  • Damian Leszczyński - Vash
    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
    sparkyspider about 13 years
    Just putting that there to clarify.
  • Joachim Sauer
    Joachim Sauer about 13 years
    qVash: why? you can simply use new ArrayList(map.keySet()) which is a more direct route and avoid creating an Enumeration. And I'm almost certain that it will perform better, because the ArrayList will be created with the correct size from the very beginning.
  • sparkyspider
    sparkyspider about 13 years
    Would this return a reference to the original values, and not a new set of objects?
  • Joachim Sauer
    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 the Map.
  • Damian Leszczyński - Vash
    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
    bcorso over 8 years
    +1 for noting proper ordering should use LinkedHashMap <=> List. Also, it's probably good practice for the unordered conversions to use HashMap <=> Set to indicate that it's unordered.
  • MAbraham1
    MAbraham1 over 8 years
    Due to its simplicity, this appears to be a better solution than the answer chosen.