Collections

How to add hashmap values to arraylist in java

public class HashMap extends AbstractMap
K – the type of keys maintained by this map
V – the type of mapped values

public V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

package com.candidjava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

public class ArrayListAddHashMap {
  public static void main(String[] args) {
    HashMap<Integer, String> hm = new HashMap<>();
    hm.put(101, "Remo");
    hm.put(102, "Rio");
    hm.put(103, "Ram");
    //Adding key into arraylist
    Set<Integer> ks = hm.keySet();
    ArrayList<Integer> al=new ArrayList<Integer>(ks);
    System.out.println(al);
    //Adding Values into arraylist
    Collection<String> value=hm.values();
    ArrayList<String> al1=new ArrayList<String>(value);
    System.out.println(al1);
        
  }

}

Output

[101, 102, 103]
[Remo, Rio, Ram]