This example shows how to convert ArrayList to LinkedHashSet in Java. This example also shows how to convert ArrayList to LinkedHashSet using Java 8 Stream, addAll method, and constructor.
How to convert ArrayList to LinkedHashSet in Java?
There are several ways using which we can convert ArrayList to LinkedHashSet object as given below.
1. Using the LinkedHashSet constructor
The LinkedHashSet class provides a constructor that accepts a Collection object.
1 |
public LinkedHashSet(Collection<? extends E> collection) |
This constructor creates a new LinkedHashSet object containing all the elements of the specified collection object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class ArrayListToLinkedHashSetExample { public static void main(String[] args) { List<Integer> aListNumber = new ArrayList<Integer>(); aListNumber.add(1); aListNumber.add(2); aListNumber.add(3); aListNumber.add(4); aListNumber.add(1); System.out.println("ArrayList contains: " + aListNumber); //use the LinkedHashSet constructor LinkedHashSet<Integer> lhSet = new LinkedHashSet<Integer>(aListNumber); System.out.println("LinkedHashSet contains: " + lhSet); } } |
Output
1 2 |
ArrayList contains: [1, 2, 3, 4, 1] LinkedHashSet contains: [1, 2, 3, 4] |
2. Using the addAll method of the LinkedHashSet class
We can also create an empty LinkedHashSet object first and then add all elements of the ArrayList to it using the addAll
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
List<Integer> aListNumber = new ArrayList<Integer>(); aListNumber.add(1); aListNumber.add(2); aListNumber.add(3); aListNumber.add(4); aListNumber.add(1); System.out.println("ArrayList contains: " + aListNumber); //create empty LinkedHashSet object LinkedHashSet<Integer> lhSet = new LinkedHashSet<Integer>(); //add all elements of ArrayList lhSet.addAll(aListNumber); System.out.println("LinkedHashSet contains: " + lhSet); |
Output
1 2 |
ArrayList contains: [1, 2, 3, 4, 1] LinkedHashSet contains: [1, 2, 3, 4] |
3. Using the Java 8 Stream
If you are using Java version 8 or later, you can also use the stream to convert List to Set as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<Integer> aListNumber = new ArrayList<Integer>(); aListNumber.add(1); aListNumber.add(2); aListNumber.add(3); aListNumber.add(4); aListNumber.add(1); System.out.println("ArrayList contains: " + aListNumber); Set<Integer> set = aListNumber.stream().collect(Collectors.toSet()); System.out.println("Set contains: " + set); |
Output
1 2 |
ArrayList contains: [1, 2, 3, 4, 1] Set contains: [1, 2, 3, 4] |
Important Note:
As we can see from the output, the ArrayList object contained element “1” twice, while the converted linked hash set object contains the element “1” only once. That is because the LinkedHashSet class is an implementation of the Set interface that does not allow duplicate elements to be added to it.
How to convert ArrayList of custom class objects to LinkedHashSet?
Let’s try it first and see what happens.
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 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } } public class ArrayListToLinkedHashSetExample { public static void main(String[] args) { //array list of custom class objects List<User> aListUsers = new ArrayList<User>(); aListUsers.add( new User(1) ); aListUsers.add( new User(1) ); aListUsers.add( new User(1) ); LinkedHashSet<User> lhSet = new LinkedHashSet<User>(aListUsers); System.out.println("LinkedHashSet contains: " + lhSet); } } |
Output
1 |
LinkedHashSet contains: [User -> 1, User -> 1, User -> 1] |
As we can see from the output when we converted ArrayList to LinkedHashSet it did not remove duplicate objects of the custom class. It is because the LinkedHashSet relies on the equals
method of its elements to compare and check if the element already exists before adding it.
Since the User class has not overridden the equals
method, the equals
method inherited from the Object class is used for comparison which compares the object references, not the actual object contents. That is the reason the equals
method returned false and the same element was added thrice.
Let’s override the equals
and hashCode
method in the User class and try again.
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 53 54 55 56 57 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; return true; } } public class ArrayListToLinkedHashSetExample { public static void main(String[] args) { //array list of custom class objects List<User> aListUsers = new ArrayList<User>(); aListUsers.add( new User(1) ); aListUsers.add( new User(1) ); aListUsers.add( new User(1) ); LinkedHashSet<User> lhSet = new LinkedHashSet<User>(aListUsers); System.out.println("LinkedHashSet contains: " + lhSet); } } |
Output
1 |
LinkedHashSet contains: [User -> 1] |
This time duplicate elements were not added to the LinkedHashSet object.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet