
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between ArrayList and HashSet in Java
In Java, ArrayList and HashSet are the most important classes of the Collection Framework. Both are used to "store collections of elements", but they are used for different purposes and have different characteristics.
ArrayList is an "ordered collection" that allows duplicate elements, while HashSet is an "unordered collection" that does not allow duplicates.
ArrayList vs HashSet in Java
Here are some key differences between ArrayList and HashSet in Java:
Key | ArrayList | HashSet |
---|---|---|
Implementation | ArrayList is the implementation of the List interface. | HashSet, on the other hand, is the implementation of a set interface. |
Internal implementation | ArrayList internally implements an array for its implementation. | HashSet internally uses HashMap for its implementation. |
Order of elements | ArrayList maintains the insertion order, i.e order of the objects in which they are inserted. | HashSet is an unordered collection and doesn't maintain any order. |
Duplicates | ArrayList allows duplicate values in its collection. | On the other hand, duplicate elements are not allowed in a HashSet. |
Index performance | ArrayList uses index for its performance, i.e, it is index-based; one can retrieve objects by calling get(index) or remove objects by calling remove(index). | HashSet is completely based on objects, also it doesn't provide a get() method. |
Null Allowed | Any number of null values can be inserted into an ArrayList without any restriction. | On the other hand, Hashset allows only one null value in its collection, after which no null value is allowed to be added. |
Creating an ArrayList in Java
The following is the syntax for creating an ArrayList in Java:
ArrayList<DataType> listName = new ArrayList<>();
Here,
- DataType: It specifies the type of elements the ArrayList can hold, such as Integer, String, etc.
- listName: The name of the ArrayList.
Creating a HashSet in Java
The following is the syntax for creating a HashSet in Java:
Set<DataType> setName = new HashSet<>();
Here,
- DataType: It specifies the type of elements the HashSet can hold, such as Integer, String, etc.
- setName: The name of the HashSet.
Example 1
The following is a basic example demonstrating the creation of an ArrayList and a HashSet and adding values to them. This example also highlights the difference: ArrayList maintains insertion order, whereas the HashSet "does not guarantee order":
import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] args) { // Creating ArrayList ArrayList<Integer> list = new ArrayList<>(); // Adding values to it list.add(10); list.add(20); list.add(30); // Printing ArrayList values System.out.println("The ArrayList values are:"); for (Integer l : list) { System.out.print(l + " "); } System.out.println(); // Creating HashSet Set<Integer> set = new HashSet<>(); // Adding values to it set.add(1); set.add(2); set.add(3); // Printing HashSet values System.out.println("The HashSet values are:"); for (Integer s : set) { System.out.print(s + " "); } } }
The above example produces the following output:
The ArrayList values are: 10 20 30 The HashSet values are: 1 2 3
Example 2
The example below clearly shows the difference between ArrayList and HashSet by allowing the insertion of duplicate values in an ArrayList, whereas not allowed in a HashSet:
import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] args) { // Creating ArrayList ArrayList<Integer> list = new ArrayList<>(); // Adding values to it list.add(10); list.add(20); list.add(30); list.add(10); // duplicate allowed // Printing ArrayList values System.out.println("The ArrayList values are:"); for (Integer l : list) { System.out.print(l + " "); } System.out.println(); // Creating HashSet Set<Integer> set = new HashSet<>(); // Adding values to it set.add(1); set.add(2); set.add(3); set.add(2); // duplicate ignored // Printing HashSet values System.out.println("The HashSet values are:"); for (Integer s : set) { System.out.print(s + " "); } } }
In the output below, you can see that HashSet does not allow the insertion of duplicate values, whereas ArrayList does:
The ArrayList values are: 10 20 30 10 The HashSet values are: 1 2 3