Difference between List and Set in Java

In this post, we will see the Difference between List and Set in Java. First, we will see List vs Set in brief later we will see each point in details.

ListSet
1.List interface maintains insertion order.1.Set doesn't maintain insertion order.
2.List interface is implemented by ArrayList,Vector and LinkedList class.2.Set interface is implemented by HashSet and LinkedHashSet class.
3.List allows duplicate element..3.Set doesn't allow duplicate element .
4.We can iterate List with help of Iterator as well as ListIterator interface.4.We can iterate Set only with iterator interface.
5.List is implemented by legacy class Vector.5.Set doesn't have legacy class.
6. We can iterate List using get() method.6. Set doesn't have get() method, we can't iterate set classes using get() method.

Difference between List and Set in Java

Let’s see all points in detail.

List interface maintains insertion order whereas Set doesn’t maintain insertion order.

Example of ArrayList –

import java.util.ArrayList;
import java.util.List;
 
public class ArraListExample {
 
	public static void main(String[] args) {
 
		List<String> listObject = new ArrayList<>();
 
		listObject.add("ram");
		listObject.add("mohan");
		listObject.add("shyam");
		listObject.add("mohan");
		listObject.add("ram");
 
		System.out.println(listObject);
	}
}

Output is – [ram, mohan, shyam, mohan, ram]

Example of HashSet –

import java.util.HashSet;
import java.util.Set;
 
public class HashSetExample {
	public static void main(String[] args) {
		Set<String> setObject = new HashSet<>();
 
		setObject.add("ram");
		setObject.add("mohan");
		setObject.add("shyam");
		setObject.add("mohan");
 
		// duplicates are not allowed but it will not give
		// any compilation error
		setObject.add("ram");
		System.out.println("set object -- " + setObject);
 
	}
}

Output is –  set object — [shyam, mohan, ram]

 

Here we can see ArrayList is maintaining insertion order whereas HashSet is not(Although LinkedHashSet maintains insertion order.

 

List interface is implemented by ArrayList, Vector and LinkedList class whereas a Set interface is implemented by HashSet and LinkedHashSet class.

In the above hierarchy, we can see ArrayList, Vector and LinkedList are major classes which implement List interface whereas  HashSet, LinkedHash and TreeSet implement Set interface.

 

List allows duplicate elements, Set doesn’t allow duplicate element.

Example –

package listandset;

import java.util.*;
public class ListAndSetExample {
public static void main(String[] args) {
	
	
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("ram");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");
	
	System.out.println("list contains duplicate elements  "+listObject);
	
	Set<String> setObject = new HashSet<>();
	 
	setObject.add("ram");
	setObject.add("mohan");
	setObject.add("shyam");
	setObject.add("mohan");
	setObject.add("ram");
	
	System.out.println("set doesn't contain duplicate elements  "+setObject);
	
}
}

Output is –

list contains duplicate elements [ram, mohan, shyam, mohan, ram]
set doesn’t contain duplicate elements [shyam, mohan, ram]

 

We can iterate List with help of Iterator as well as ListIterator interface whereas we can iterate Set only with iterator interface.

Example –

package listandset;

import java.util.*;
public class ListAndSetExample {
public static void main(String[] args) {
	
	
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("ram");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");
	
	Iterator<String> it = listObject.iterator();
	//Iterating listObject through Iterator
	
	while(it.hasNext()) {
		System.out.println(it.next());
	}
	
	//Iterating listObject through ListIterator
	ListIterator<String> lit = listObject.listIterator();
	while(lit.hasNext()) {
		System.out.println(lit.next());
	}
	
	
	Set<String> setObject = new HashSet<>();
	 
	setObject.add("ram");
	setObject.add("mohan");
	setObject.add("shyam");
	setObject.add("mohan");
	setObject.add("ram");
	
	Iterator<String> itForSet = setObject.iterator();
	
	//Iterating listObject through ListIterator
	while(itForSet.hasNext()) {
		System.out.println(itForSet.next());
	}
	
	
	ListIterator<String> itlForSet = setObject.listIterator();
	
	
	
	
}
}

 

In the above example line number 46 will give a compilation error.

We can iterate List using get() method whereas we can’t iterate set classes using get() method.

package listandset;

import java.util.*;
public class ListAndSetExample {
public static void main(String[] args) {
	
	
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("ram");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");
	
	for(int i= 0; i<listObject.size(); i++) {
		System.out.println(listObject.get(i));
	}
	
	Set<String> setObject = new HashSet<>();
	 
	setObject.add("ram");
	setObject.add("mohan");
	setObject.add("shyam");
	setObject.add("mohan");
	setObject.add("ram");
	
	
	for(int i= 0; i<setObject.size(); i++) {
		
		//No way you can't do this
		System.out.println(setObject.get(i));
	}
	
	
	
}
}

In above example line number 32 will  compilation error.

Tha’s all about the difference between List and Set in Java.

You may like.