Java – List v/s Set

In this article, we will discuss difference between List and Set in detail i.e.; List v/s Set. Both interfaces directly extends Collection interface

1. List v/s Set:

ListSet
List stores elements according to insertion order

 

So, insertion order is preserved

Set stores elements in random order, as it uses hashing technique

 

Insertion order isn’t preserved

While iterating List items, elements will be retrieved as per insertion orderWhile iterating Set items, elements will be retrieved in random order
List allows duplicate elementsSet doesn’t allow duplicate elements i.e.; it stores only unique elements

 

Note: if same element is added again, there won’t be any compile-time or runtime error, just that add() method returns false;

Any number of NULL object is allowed to add to the ListMaximum of one NULL is allowed

Q) When to use List ?

  • If the business requirement is to preserve insertion order and
  • adding duplicate elements is not a big concern
  • then List is the good choice to store group of elements
  • Example: it could be ArrayList or LinkedList or Vector, etc

Q) When to use Set ?

  • If the business requirement is to avoid storing duplicate elements
  • And storing only unique elements
  • Where insertion order isn’t big factor while iterating items
  • then Set is the good choice to store group of elements
  • Example: it could be HashSet, etc

2. List Program using ArrayList, LinkedList and Vector :

ListPrograms.java

package in.bench.resources.collection;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

public class ListPrograms {

	public static void main(String[] args) {

		// create object of ArrayList using List reference
		List<String> arrayList = new ArrayList<String>();
		arrayList.add("Sachin");
		arrayList.add("Sourav");
		arrayList.add("Dravid");
		arrayList.add("Laxman");

		System.out.println("ArrayList elements : "
				+ arrayList);

		// create object of LinkedList using List reference
		List<String> linkedList = new LinkedList<String>();
		linkedList.add("Sehwag");
		linkedList.add("Yuvraj");
		linkedList.add("Zaheer");
		linkedList.add("Harbhajan");

		System.out.println("\nLinkedList elements : "
				+ linkedList);

		// create object of Vector using List reference
		List<String> vector = new Vector<String>();
		vector.add("Kumble");
		vector.add("Srinath");
		vector.add("Azhar");

		System.out.println("\nVector elements : "
				+ vector);
	}
}

Output:

ArrayList elements : [Sachin, Sourav, Dravid, Laxman]

LinkedList elements : [Sehwag, Yuvraj, Zaheer, Harbhajan]

Vector elements : [Kumble, Srinath, Azhar]

3. Set Program using HashSet, LinkedHashSet and TreeSet :

SetPrograms.java

package in.bench.resources.collection;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class SetPrograms {

	public static void main(String[] args) {

		// create object of HashSet using Set reference
		Set<String> hashSet = new HashSet<String>();
		hashSet.add("Sachin");
		hashSet.add("Sourav");
		hashSet.add("Dravid");
		hashSet.add("Laxman");

		System.out.println("HashSet elements : "
				+ hashSet);

		// create object of LinkedHashSet using Set reference
		Set<String> linkedHashSet = new LinkedHashSet<String>();
		linkedHashSet.add("Sehwag");
		linkedHashSet.add("Yuvraj");
		linkedHashSet.add("Zaheer");
		linkedHashSet.add("Harbhajan");

		System.out.println("\nLinkedHashSet elements : "
				+ linkedHashSet);

		// create object of TreeSet using Set reference
		Set<String> treeSet = new TreeSet<String>();
		treeSet.add("Kumble");
		treeSet.add("Srinath");
		treeSet.add("Azhar");

		System.out.println("\nTreeSet elements : "
				+ treeSet);
	}
}

Output:

HashSet elements : [Dravid, Laxman, Sourav, Sachin]

LinkedHashSet elements : [Sehwag, Yuvraj, Zaheer, Harbhajan]

TreeSet elements : [Azhar, Kumble, Srinath]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - HashSet v/s LinkedHashSet v/s TreeSet
Java - TreeSet class with example