Differences between Set and List in Java
Set in Java:
The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates are prohibited. The mathematical set is produced using the set interface. The set interface uses the collection interface's methods to prevent the insertion of duplicate elements. There are two interfaces that improve the set implementation: SortedSet and NavigableSet.
Syntax:
Set<DataType/Wrapper class> variableName = new Set< > ();
Set Methods:
All the methods from the Collection interface are included in the Set interface. This is because the Collection interface is the super-interface of the Set collection.
Method | Description |
add () | Adds the desired element to the set using the add () method. |
addAll() | Adds every component of the supplied collection to the set using the addAll () method. |
Iterator () | provides a return value that can be used to access the set's elements successively. |
remove () | Removes the requested element from the set using remove (). |
removeAll () | eliminates every element from the set that is also present in another set that is supplied. |
retainAll () | keeps all set elements that are also present in another set that is given. |
Clear () | eliminates every element from the set. |
Size () | yields the set's length (the number of entries). |
toArray () | provides an array with each element of the set. |
contains () | If the set contains the specified element, the function contains () returns true. |
Set Operations:
Consider two sets (say set c and set d),
We can carry out fundamental set operations like Union, Intersection, and subset using the Java Set interface.
Using c.addAll (d), we can find the Union of two sets, c and d. Using x, we can find the Intersection of two sets, c and d.
Using d.containsAll (c), we can determine whether c is a subset of d. (c).
Union of Sets:
UnionSet.java
import java.util.*;
import java.io.*;
class UnionSet {
public static void main (String [] args) {
HashSet <Integer> eNums = new HashSet<> ();
eNums.add (2);
eNums.add (4);
System.out.println ("HashSet1: " + eNums);
HashSet <Integer> nums = new HashSet<>();
nums.add (1);
nums.add (3);
System.out.println ("HashSet2: " + nums);
//Union of the given sets
nums.addAll (eNums);
System.out.println ("Union is: " + nums);
}
}
Output:

The Intersection of sets:
import java.io.*;
import java.util.*;
import java.util.HashSet;
class IntersectSet {
public static void main (String[] args) {
HashSet <Integer> pNums = new HashSet<>();
pNums.add (2);
pNums.add (3);
System.out.println ("HashSet1: " + pNums);
HashSet <Integer> eNums = new HashSet<> ();
eNums.add (2);
eNums.add (4);
System.out.println ("HashSet2: " + eNums);
//Intersection of two sets
eNums.retainAll (pNums);
System.out.println ("Intersection is: " + eNums);
}
}

HashSet in Java:
A collection with hash table storage is made using the Java HashSet class. It implements the Set interface and derives from the AbstractSet class.
The following are the key features of the Java HashSet class:
- HashSet uses the hashing technique to store the components.
- HashSet only has distinct components.
- HashSet accepts a null value.
- The HashSet class lacks synchronization.
The insertion order is not maintained by HashSet. The hashcodes of the elements are used to insert them in this place.
The optimum method for search operations is HashSet.
HashSet's load factor is 0.75, and its initial default capacity is 16.
HashSet Program:
HashingSet.java
import java.io.*;
import java.util.*;
import java.lang.*;
class Solution {
public int findDuplicate (int [] nums) {
HashSet <Integer> p = new HashSet<> ();
// creation of Hashset
int rep = 0;
// entering numbers into the hash set if a number is with its first frequency
for (int i = 0; i < nums.length; i++)
{
if (p.contains (nums [i]))
{
rep = nums [i];
break;
// if a number is already found in the hash set
// then it breaks from the loop and returns the duplicate element
}
p.add (nums [i]);
}
return rep;
}
}
class HashingSet
{
public static void main ( String args [])
{
Scanner scan = new Scanner (System.in);
System.out.println ("enter the value of n");
int n = scan.nextInt ();
System.out.println ("enter the array elements");
int [] arr = new int [n];
for (int I = 0; i < n; i++)
{
arr [i] = scan.nextInt ();
}
Solution obj =new Solution ();
System.out.println ("The duplicate element is " +obj.findDuplicate (arr));
}
}
Output:

List in Java:
Java's list feature makes it possible to keep an organized collection. It includes index-based techniques for adding, updating, deleting, and searching components. Duplicate elements are also possible. The List can also contain null elements.
The Collection interface is inherited by the List interface, which may be found in Java. util package. It is the interface's ListIterator factory. We can iterate the List both forward and backwards using the ListIterator. The ArrayList, LinkedList, Stack, and Vector classes are the implementation classes for the List interface. In Java programming, the ArrayList and LinkedList are frequently used. Since Java 5, the Vector class has been deprecated.
Syntax:
List<Object> list = new ArrayList<Object> ();
The List is an interface. Hence objects of the type list cannot be made. To build an object, we always require a class that implements this List. Additionally, since Generics were added in Java 1.5, it is now possible to limit the kinds of objects that can be placed in a List. The List is a user-defined "interface" that is implemented by the ArrayList class, which is pre-defined in the Java.util package, just as a number of other user-defined "interfaces" by user-defined "classes".
Array List Program in Java:
ArrayListDemo.java
import java.io.*;
import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
ArrayList<String> a=new ArrayList<String>();
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
a.add("APRICOT");
a.add("MANGO");
a.add("GRAPES");
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
System.out.println("\n1-using for-each loop");
for(String i:a)
{
System.out.println(i);
}//i
a.add("GUAVA");
a.add("BANANA");
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
System.out.println("\n2-using iterator");
Iterator it=a.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
a.add("APPLE");
a.add("ORANGE");
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
System.out.println("\n3.1-using ListIterator in forward direction");
ListIterator lit=a.listIterator();
while(lit.hasNext())
{
System.out.println(lit.next());
}//while
System.out.println("\n3.2-using ListIterator in backward direction");
while(lit.hasPrevious())
{
System.out.println(lit.previous());
}//while
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
a.remove(3);
a.remove("APPLE");
System.out.println("the size of the ArrayList is " +a.size());
System.out.println("the objects of ArrayList are " +a);
}//main
}//ArrayListDemo
Output:

Differences between set and list:
The List and the Set are both parts of the Collection framework in Java. Set and List interfaces are used to store the collection of objects as a single entity. In addition to these similarities, both interfaces have the following differences:
S.no | SET | LIST |
1. | Here, we cannot add identical or duplicate elements using the set implementation. | Here, we can add identical or duplicate elements using the list implementation. |
2. | Order of inserting elements is not maintained by set interface. | List interface maintains the insertion order of elements. |
3. | Insertion of null values is not allowed, at least one null value is allowed by set interface. | Insertion of null values are allowed in lists. |
4. | Set implementation classes include HashSet, TreeSet and LinkedHashSet. | List implementation classes include LinkedList and ArrayList. |
5. | Set interface does not provide a get method, we are unable to locate an element from the Set based on the index (). | The get () method allows us to retrieve an element from a list at a specific index. |
6. | When we want to create a collection of unique elements, we use it. | It is utilized when we frequently need to use the index to access the elements. |
7. | When we need to iterate the Set elements, we use the iterator. | The elements of the List are iterated using the listiterator () method of the List interface. |