Is a HashMap a list Java?

HashMap and ArrayList are two most used data structures in java. Both classes inherit from different hierarchies. HashMap is inherited from Map interface which represents the data in the form of key-value pairs. ArrayList is inherited from List interface which arranges the data in the sequential manner. Conversion of HashMap to ArrayList has also become a regular question in the java interviews as there is no direct methods in HashMap which converts theHashMap to ArrayList. In this post, we will see how to convert HashMap to ArrayList in java with examples. At the end, we will also see java 8 code to convert map to list in java.

How To Convert HashMap To ArrayList In Java?

As HashMap contains key-value pairs, there are three ways you can convert given HashMap to ArrayList. You can convert HashMap keys into ArrayList or you can convert HashMap values into ArrayList or you can convert key-value pairs into ArrayList.Lets see these three methods in detail.

a] Conversion Of HashMap Keys Into ArrayList :

For this, we use keySet[] method of HashMap which returns the Set containing all keys of the HashMap. And then we pass this Set while constructing the ArrayList.

//Creating a HashMap object HashMap map = new HashMap[]; //Getting Set of keys from HashMap Set keySet = map.keySet[]; //Creating an ArrayList of keys by passing the keySet ArrayList listOfKeys = new ArrayList[keySet];

b] Conversion Of HashMap Values Into ArrayList :

For this, we use values[] method of HashMap which returns the Collection containing all values of theHashMap. Then we usethisCollection tocreate theArrayList of values.

//Creating a HashMap object HashMap map = new HashMap[]; //Getting Collection of values from HashMap Collection values = map.values[]; //Creating an ArrayList of values ArrayList listOfValues = new ArrayList[values];

c] Conversion Of HashMaps Key-Value Pairs Into ArrayList :

For this, we use entrySet[] method of HashMap which returns the Set of Entry objects where each Entry object represents one key-value pair. We pass thisSetto createthe ArrayList of key-value pairs.

//Creating a HashMap object HashMap map = new HashMap[]; //Getting Set of entries from HashMap Set entrySet = map.entrySet[]; //Creating an ArrayList of Entry objects ArrayList listOfEntry = new ArrayList[entrySet];

Java Program To Convert HashMap To ArrayList :

Below example converts the studentPerformanceMap to listOfKeys, listOfValues and listOfEntry.

import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; public class Java8MapToListExamples { public static void main[String[] args] { //Creating a HashMap object HashMap studentPerformanceMap = new HashMap[]; //Adding elements to HashMap studentPerformanceMap.put["John Kevin", "Average"]; studentPerformanceMap.put["Rakesh Sharma", "Good"]; studentPerformanceMap.put["Prachi D", "Very Good"]; studentPerformanceMap.put["Ivan Jose", "Very Bad"]; studentPerformanceMap.put["Smith Jacob", "Very Good"]; studentPerformanceMap.put["Anjali N", "Bad"]; //Getting Set of keys Set keySet = studentPerformanceMap.keySet[]; //Creating an ArrayList of keys ArrayList listOfKeys = new ArrayList[keySet]; System.out.println["ArrayList Of Keys :"]; for [String key : listOfKeys] { System.out.println[key]; } System.out.println["--------------------------"]; //Getting Collection of values Collection values = studentPerformanceMap.values[]; //Creating an ArrayList of values ArrayList listOfValues = new ArrayList[values]; System.out.println["ArrayList Of Values :"]; for [String value : listOfValues] { System.out.println[value]; } System.out.println["--------------------------"]; //Getting the Set of entries Set entrySet = studentPerformanceMap.entrySet[]; //Creating an ArrayList Of Entry objects ArrayList listOfEntry = new ArrayList[entrySet]; System.out.println["ArrayList of Key-Values :"]; for [Entry entry : listOfEntry] { System.out.println[entry.getKey[]+" : "+entry.getValue[]]; } } }

Output :

ArrayList Of Keys :
Rakesh Sharma
Anjali N
Smith Jacob
John Kevin
Ivan Jose
Prachi D

ArrayList Of Values :
Good
Bad
Very Good
Average
Very Bad
Very Good

ArrayList of Key-Values :
Rakesh Sharma : Good
Anjali N : Bad
Smith Jacob : Very Good
John Kevin : Average
Ivan Jose : Very Bad
Prachi D : Very Good

Java 8 Convert Map To List

a] Java 8 Convert Map Keys To List

//Creating a Map object Map map = new HashMap[]; //Java 8 code to convert map keys to list List listOfKeys = map.keySet[].stream[].collect[Collectors.toList[]]; //Java 8 code to print List elements listOfKeys.forEach[System.out::println];

b] Java 8 Convert Map Values To List

//Creating a Map object Map map = new HashMap[]; //Java 8 code to convert map values to list List listOfValues = map.values[].stream[].collect[Collectors.toList[]]; //Java 8 code to print List elements listOfValues.forEach[System.out::println];

c] Java 8 Sort And Convert Map Keys To List

//Creating a Map object Map map = new HashMap[]; //Java 8 code to sort and convert map keys to list List listOfKeys = map.keySet[].stream[].sorted[].collect[Collectors.toList[]]; //Java 8 code to print List elements listOfKeys.forEach[System.out::println];

d] Java 8 Sort And Convert Map Values To List

//Creating a Map object Map map = new HashMap[]; //Java 8 code to sort and convert map values to list List listOfValues = map.values[].stream[].sorted[].collect[Collectors.toList[]]; //Java 8 code to print List elements listOfValues.forEach[System.out::println];

Note : You can also provide Comparator to sorted[] method to sort the keys or values as you wish.

Also Read :

How HashMap works internally in java?

What is load factor and initial capacity of HashMap?

15 Java HashMap Programs And Examples

References :

  • HashMap
  • ArrayList

Video liên quan

Chủ Đề