Here are couple of differences between ArrayList and HashSet.
Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.
ArrayList is backed by an Array while HashSet is backed by an HashMap.
ArrayList allows duplicate values while HashSet doesn’t allow duplicates values.
ArrayList have three constructor which are ArrayList(), ArrayList(int capacity) ArrayList(int Collection c) while HashSet have four constructor which are HashSet(), HashSet(int capacity), HashSet(Collection c) and HashSet(int capacity, float loadFactor)
ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn’t maintain any order.
ArrayList is index based we can retrieve object by calling get(index) method or remove objects by calling remove(index) method while HashSet is completely object based. HashSet also does not provide get() method.
ArrayList not apply any restriction, we can add any number of null value while HashSet allow one null value.
ArrayList:-ArrayList list=new ArrayList();
HashSet:-HashSet set=new HashSet();
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
1 2 3 4
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Convert HashSet to a ArrayList in Java
- ArrayList of ArrayList in Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Difference between HashMap and HashSet
- Java Collection| Difference between Synchronized ArrayList and CopyOnWriteArrayList
- Difference between length of Array and size of ArrayList in Java
- Difference between List and ArrayList in Java
- Check whether array has all identical elements using Arrays.asList() and HashSet in Java
- Difference between ArrayList and CopyOnWriteArrayList
- HashSet in Java
- Internal working of Set/HashSet in Java
- Initializing HashSet in Java
- HashSet contains() Method in Java
- HashSet clear() Method in Java
- HashSet isEmpty() Method in Java
- HashSet iterator() Method in Java
- HashSet remove() Method in Java
- HashSet size() Method in Java
- HashSet add() Method in Java
- HashSet clone() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.