This example shows how to convert ArrayList to HashSet in Java. The example also shows how to convert using the HashSet constructor, addAll method, and Java 8 stream.
How to convert an ArrayList to HashSet in Java?
There are several ways using which you can convert an ArrayList object to the HashSet object in Java as given below.
1. Using the addAll method
The addAll
method of the HashSet class adds all elements of the specified collection to this set object.
1 |
public boolean addAll(Collection<? extends E> collection) |
It returns true if this set is changed as a result of the method call, false otherwise.
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 |
import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class ArrayListToHashSetExample { public static void main(String[] args) { //ArrayList object List<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(1); HashSet<Integer> hsetNumbers = new HashSet<Integer>(); /* * Use addAll method to add all elements of the * ArrayList to HashSet */ hsetNumbers.addAll(aListNumbers); System.out.println("HashSet contains: " + hsetNumbers); } } |
Output
1 |
HashSet contains: [1, 2, 3] |
Tip: Use this approach when you already have an existing empty object of the HashSet class.
2. Using the constructor
We can also use the HashSet constructor that accepts Collection as an argument.
1 |
public HashSet(Collection<? extends E> collection) |
This constructor creates a new HashSet object having the elements contained in the specified collection object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//ArrayList object List<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(1); /* * Use HashSet constructor to create a new * HashSet object having all elements of the ArraList */ HashSet<Integer> hsetNumbers = new HashSet<Integer>( aListNumbers ); System.out.println("HashSet contains: " + hsetNumbers); |
Output
1 |
HashSet contains: [1, 2, 3] |
Tip: Use this approach when you do not have an existing HashSet object.
3. Using the stream (Java 8)
We can also use the Java stream to convert List to Set as given below.
1 2 3 4 5 6 7 8 9 10 11 |
//ArrayList object List<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(1); Set<Integer> setNumbers = aListNumbers.stream().collect(Collectors.toSet()); System.out.println("Set contains: " + setNumbers); |
Output
1 |
Set contains: [1, 2, 3] |
Important Note: As we can see from all the outputs, only unique elements from the ArrayList are added to the HashSet object.
Converting ArrayList of custom objects to a HashSet object
The HashSet class relies on the equals
method to prevent duplicate elements from being added to it. When we convert an ArrayList containing duplicate objects of a custom class, and if the custom class has not implemented the equals
and hashCode
methods, duplicate objects will be added to the HashSet too.
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 |
import java.util.ArrayList; import java.util.HashSet; import java.util.List; class Student{ int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "StudentID ->" + getId(); } } public class ArrayListToHashSetExample { public static void main(String[] args) { List<Student> aListStudents = new ArrayList<Student>(); aListStudents.add(new Student(1)); aListStudents.add(new Student(1)); HashSet<Student> hsetStudents = new HashSet<Student>( aListStudents ); System.out.println("HashSet contains: " + hsetStudents); } } |
Output
1 |
HashSet contains: [StudentID ->1, StudentID ->1] |
To solve this duplication problem, we need to implement the equals
and hashCode
method in our custom class as given below.
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 |
import java.util.ArrayList; import java.util.HashSet; import java.util.List; class Student{ int id; public Student(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "StudentID ->" + 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; Student other = (Student) obj; if (id != other.id) return false; return true; } } public class ArrayListToHashSetExample { public static void main(String[] args) { List<Student> aListStudents = new ArrayList<Student>(); aListStudents.add(new Student(1)); aListStudents.add(new Student(1)); HashSet<Student> hsetStudents = new HashSet<Student>( aListStudents ); System.out.println("HashSet contains: " + hsetStudents); } } |
Output
1 |
HashSet contains: [StudentID ->1] |
As we can see from the output, now only unique custom class objects are added to the HashSet.
This example is a part of the Java HashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet