This example shows how to convert an ArrayList to LinkedHashMap in Java. The example also shows how to convert ArrayList to LinkedHashMap using Java 8 stream.
How to convert ArrayList to LinkedHashMap in Java?
The ArrayList and LinkedHashMap are two different data structures. The ArrayList is a resizable array implementation of the List interface while the LinkedHashMap is an implementation of the Map interface that maps keys to values.
Suppose you have an ArrayList containing the objects of the Student class that you want to convert to a LinkedHashMap. The LinkedHashMap maps keys to corresponding values. In this case, you can use the student id as a key and the Student object as the value of the LinkedHashMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.ArrayList; import java.util.LinkedHashMap; class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public Integer getId(){ return this.id; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } } public class ArrayListToLinkedHashMapExample { public static void main(String[] args) { ArrayList<Student> aListStudent = new ArrayList<Student>(); aListStudent.add(new Student(1, "Jack")); aListStudent.add(new Student(2, "John")); aListStudent.add(new Student(3, "Emily")); aListStudent.add(new Student(4, "Maria")); System.out.println("ArrayList contains: " + aListStudent); //create new empty LinkedHashMap LinkedHashMap<Integer, Student> lhmap = new LinkedHashMap<Integer, Student>(); /* * To convert ArrayList to LinkedHashMap, iterate * over the ArrayList and add unique id and arraylist * element to the LinkedHashMap */ for(Student student : aListStudent){ //add student id as key and student object as value of LinkedHashMap lhmap.put(student.getId(), student); } System.out.println("LinkedHashMap contains: " + lhmap); } } |
Output
1 2 |
ArrayList contains: [[1=>Jack], [2=>John], [3=>Emily], [4=>Maria]] LinkedHashMap contains: {1=[1=>Jack], 2=[2=>John], 3=[3=>Emily], 4=[4=>Maria]} |
How to convert to LinkedHashMap using Java 8?
If you are using Java version 8 or above, you can use the stream to convert to a LinkedHashMap object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<Student> aListStudent = new ArrayList<Student>(); aListStudent.add(new Student(1, "Jack")); aListStudent.add(new Student(2, "John")); aListStudent.add(new Student(3, "Emily")); aListStudent.add(new Student(4, "Maria")); System.out.println("ArrayList contains: " + aListStudent); Map<Integer, Student> mapStudents = aListStudent.stream().collect( Collectors.toMap(Student::getId, student->student)); System.out.println("Map contains: " + mapStudents); |
Output
1 2 |
ArrayList contains: [[1=>Jack], [2=>John], [3=>Emily], [4=>Maria]] Map contains: {1=[1=>Jack], 2=[2=>John], 3=[3=>Emily], 4=[4=>Maria]} |
Important Note:
In the above examples, I used the Student class that has id as a unique field so it was used as the LinkedHashMap key. Before converting, identify the unique field in your class and use it as the key of the map object.
Since the LinkedHashMap class maps a key to a value and cannot have duplicate keys, If the ArrayList has duplicate elements, the converted LinkedHashMap will contain only one entry for the given duplicated key.
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap