ArrayList is a dynamic array.As we discussed in pervious post, ArrayList is slower than arrays but there is no restriction on size.Based on requirement, we can keep growing the elements in the list.
HasMap is used for storing Key/Value as elements.But order of the elements is the same as we store the elements in it.Since it uses hashing so elements order is not predictable.
There are two ways to convert ArrayList to HashMap.
- Before Java 8 conversion
- With or after Java 8
ArrayList to HashMap conversion before Java 8
Before Java 8 we need to iterate elements of ArrayList and store individual elements to Map.So basically this process depends on iteration.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ListToMap {
public static void main(String[] args) {
ArrayList<String> fruitList
= new ArrayList<>(Arrays.asList(
"Banana", "Mango", "Orange", "Apple", "Grapes"));
System.out.println("Fruits List:");
for (String fruits : fruitList) {
System.out.println(fruits);
}
System.out.println("HashMap:");
HashMap<String, Integer> fruitsMap
= convertListtoMap(fruitList);
for (Map.Entry<String, Integer> entry :
fruitsMap.entrySet()) {
System.out.println(entry.getKey() + " : "
+ entry.getValue());
}
}
private static HashMap<String, Integer>
convertListtoMap(ArrayList<String> arrayList) {
HashMap<String, Integer> hashMap = new HashMap<>();
for (String item : arrayList) {
hashMap.put(item, item.length());
}
return hashMap;
}
}
Output:
Fruits List:
Banana
Mango
Orange
Apple
Grapes
HashMap:
Apple : 5
Grapes : 6
Mango : 5
Orange : 6
Banana : 6
ArrayList to HashMap conversion with Java 8
For this we will be using java streams.
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
class Student {
int rollNumber;
String studentName;
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
public class ConvertListToMapUsingJava8 {
public static void main(String[] args) {
ArrayList<Student> studentList
= new ArrayList<>();
//Creating student to store in arraylist
Student student = new Student();
student.setStudentName("Smith");
student.setRollNumber(1);
//storing student 1 to arraylist
studentList.add(student);
Student student2 = new Student();
student2.setStudentName("Alex");
student2.setRollNumber(2);
//storing student 2 to arraylist
studentList.add(student2);
System.out.println("Student List: ");
// printing list elements
for (int i = 0; i < studentList.size(); i++) {
System.out.print("RollNumber: " + studentList.get(i).rollNumber);
System.out.println(" Name: " + studentList.get(i).studentName);
}
//converting list to Map using java 8
Map<String, Integer> studentsMap = null;
studentsMap = studentList.stream().collect(Collectors.toMap(Student::getStudentName, Student::getRollNumber));
System.out.println("Students Map: " + studentsMap);
}
}
Output:
Student List:
RollNumber: 1 Name: Smith
RollNumber: 2 Name: Alex
Students Map: {Alex=2, Smith=1}