C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ArrayList: We create an ArrayList of Entry elements. The Entry has the same key and value types as the HashMap we just created.
EntrySet: Use call addAll() on the ArrayList to add the entire entrySet to it. The ArrayList now contains all the HashMap's elements.
Java program that converts HashMap to ArrayList
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// Create a HashMap.
HashMap<Integer, String> hash = new HashMap<>();
hash.put(100, "one-hundred");
hash.put(1000, "one-thousand");
hash.put(50, "fifty");
// Use Entry type.
// ... Create an ArrayList and call addAll to add the entrySet.
ArrayList<Entry<Integer, String>> array = new ArrayList<>();
array.addAll(hash.entrySet());
// Loop over ArrayList of Entry elements.
for (Entry<Integer, String> entry : array) {
// Use each ArrayList element.
int key = entry.getKey();
String value = entry.getValue();
System.out.println("Key = " + key + "; Value = " + value);
}
}
}
Output
Key = 50; Value = fifty
Key = 100; Value = one-hundred
Key = 1000; Value = one-thousand
GetKey, getValue: To get keys and values (in this example, Integers and Strings) we must use getKey and getValue on each Entry.