How to Convert ArrayList to HashMap Before Java 8?
ArrayList is a resizable array. It is under the java package java.util. It gives dynamic arrays in Java. ArrayList is useful in programs where a lot of changes in the array are required but these are also slower than the standard arrays. The elements in an ArrayList can be added and removed whenever needed.
HashMap is a data structure that stores items in “Key/Value” pairs. They can be accessed by an index of another type like a String. The HashMap is denoted as HashMap<Key, Value>. This is very similar to HashTable, but it permits the use of null values and null keys. It does not return the Keys and Values in the same order in which they are stored into the HashMap. It is also under the java package java.util.
Now in order to convert ArrayList to HashMap both use cases are considered to illustrate override concept as well
- Initial ArrayList is having no duplicates
- Initial ArrayList is having duplicate values.
Case 1: The most ideal way to convert an ArrayList into HashMap before Java 8 is through iterating over the given ArrayList using enhanced for-loop and inserting the String as the key and its length as the value into HashMap. Here, each element of the ArrayList can be converted into a key-value pair and store in the HashMap. This method also handles any duplicate in the ArrayList as well because of inserted entries that override values in case of duplicate keys. In this case, no error or exception is thrown.
Example: Initial ArrayList is having no duplicates
Java
// Program to convert ArrayList // to Hashmap before Java 8 // Name of ArrayList chosen randomly: vehicleList // Importing java generic libraries import java.util.*; public class GFG { // Main driver method public static void main(String[] args) { // Creating ArrayList ArrayList<String> vehicleList = new ArrayList<>(Arrays.asList( "Car" , "Bike" , "Bus" , "Cycle" , "Rickshaw" )); // Display message to enter ArrayList System.out.println( "ArrayList: \n" ); // Printing ArrayList for (String vehicle : vehicleList) { System.out.println(vehicle); } // Display message to separate above // list from hashmap for readability System.out.println( "\nHashMap: \n" ); // Creating Hashmap HashMap<String, Integer> vehicleMap = convertArrayListToHashMap(vehicleList); for (Map.Entry<String, Integer> entry : vehicleMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } // Converting ArrayList to HashMap private static HashMap<String, Integer> convertArrayListToHashMap(ArrayList<String> arrayList) { HashMap<String, Integer> hashMap = new HashMap<>(); // For-each loop for iteration for (String str : arrayList) { hashMap.put(str, str.length()); } // Returning converted Hashmap return hashMap; } } |
ArrayList: Car Bike Bus Cycle Rickshaw HashMap: Bus : 3 Rickshaw : 8 Car : 3 Bike : 4 Cycle : 5
Case 2: Initial ArrayList is having duplicate values where it overrides the value due to a duplicate key in HashMap.
Java
// Program to convert ArrayList // to Hashmap before Java 8 // Importing java generic libraries import java.util.*; public class GFG { // Main driver method public static void main(String[] args) { // Creating ArrayList ArrayList<String> vehicleList = new ArrayList<>( // Presence of Duplicate Elements Arrays.asList( "Car" , "Bike" , "Bus" , "Cycle" , "Rickshaw" , "Car" , "Bike" )); System.out.println( "ArrayList: \n" ); // Printing ArrayList for (String vehicle : vehicleList) { System.out.println(vehicle); } // Display message just after ArrayList is printed // for better readability System.out.println( "\nHashMap: \n" ); // Creating HashMap HashMap<String, Integer> vehicleMap = convertArrayListToHashMap(vehicleList); // Printing above HashMap for (Map.Entry<String, Integer> entry : vehicleMap.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } // Converting ArrayList to HashMap private static HashMap<String, Integer> convertArrayListToHashMap(ArrayList<String> arrayList) { HashMap<String, Integer> hashMap = new HashMap<>(); // For-each loop for iteration for (String str : arrayList) { hashMap.put(str, str.length()); } // Returning converted HashMap return hashMap; } } |
ArrayList: Car Bike Bus Cycle Rickshaw Car Bike HashMap: Bus : 3 Rickshaw : 8 Car : 3 Bike : 4 Cycle : 5
Contact Us