# 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.

### 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<> ();
System.out.println ("HashSet1: " + eNums);

HashSet <Integer> nums = new HashSet<>();
System.out.println ("HashSet2: " + nums);

//Union of the given sets
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<>();
System.out.println ("HashSet1: " + pNums);

HashSet <Integer> eNums = new HashSet<> ();
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
}
}
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);
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
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());
}
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: