Difference between ArrayList and HashSet in Java - GeeksforGeeks

11 Dec, 2018
Here are couple of differences between ArrayList and HashSet.
Inheritance:Implementation:Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.Internal implementation:ArrayList is backed by an Array while HashSet is backed by an HashMap.Duplicates :ArrayList allows duplicate values while HashSet doesn’t allow duplicates values.Constructor :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)Ordering :ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn’t maintain any order.Indexing :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.Null Object:ArrayList not apply any restriction, we can add any number of null value while HashSet allow one null value.Syntax:ArrayList:-ArrayList list=new ArrayList();HashSet:-HashSet set=new HashSet();
Inheritance:
Implementation:Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.
Internal implementation:ArrayList is backed by an Array while HashSet is backed by an HashMap.
Duplicates :ArrayList allows duplicate values while HashSet doesn’t allow duplicates values.
Constructor :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)
Ordering :ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn’t maintain any order.
Indexing :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.
Null Object:ArrayList not apply any restriction, we can add any number of null value while HashSet allow one null value.
Syntax:ArrayList:-ArrayList list=new ArrayList();HashSet:-HashSet set=new HashSet();
HashSet:-
ArrayList example
// Java program to demonstrate working of ArrayList in Java  import java.io.*;import java.util.*;  class ArrayListTest {      public static void main(String[] args)        throws IOException    {        // size of ArrayList        int n = 5;          // declaring ArrayList with initial size n        List<Integer> al = new ArrayList<>(n);          // Appending the new element at the end of the list        for (int i = 1; i <= n; i++) {            al.add(i);        }          // Printing elements        System.out.println(al);          // Remove element at index 3        al.remove(3);          // Displaying ArrayList after deletion        System.out.println(al);          // Printing elements one by one        for (int i = 0; i < al.size(); i++) {            System.out.print(al.get(i) + " ");        }    }}
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 

HashSet example
// Java program to demonstrate working of HashSet  import java.util.HashSet;import java.util.Set;  class HashSetDemo {      public static void main(String[] args)    {          // Create a HashSet        Set<Integer> hs = new HashSet<>();          // add elements to HashSet        hs.add(1);        hs.add(2);        hs.add(3);        hs.add(4);          // Duplicate removed        hs.add(4);          // Displaying HashSet elements        for (Integer temp : hs) {            System.out.print(temp + " ");        }    }}
Output:
1 2 3 4 

Java-ArrayList
Java-Collections
java-hashset
Java-List-Programs
Java-Set-Programs
Technical Scripter 2018
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between BFS and DFS
Difference between SQL and NoSQL
Class method vs Static method in Python
Difference between var, let and const keywords in JavaScript
Difference between Repeater and Amplifier
Arrays in Java
HashMap getOrDefault(key, defaultValue) method in Java with Examples
JRadioButton | Java Swing
HashMap get() Method in Java
Queue Interface In Java