What Difference between List and Set in Java ?

In this tutorial we will learn what difference between List and Set in java and when to use List and when to use Set ?

List and Set both are interfaces. They both extends Collection interface. In this post we are discussing the differences between List and Set interfaces in java.

List VS Set

1) List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list.

Set is an unordered collection, it doesn’t maintain any order. There are few implementations of Set which maintains the order such as LinkedHashSet (It maintains the elements in insertion order).

2) List allows duplicates while Set doesn’t allow duplicate elements. All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value.

3) List implementations: ArrayList, LinkedList etc.

Set implementations: HashSet, LinkedHashSet, TreeSet etc.

4) List allows any number of null values. Set can have only a single null value at most.

5) ListIterator can be used to traverse a List in both the directions(forward and backward) However it can not be used to traverse a Set. We can use Iterator (It works with List too) to traverse a Set.

6) List interface has one legacy class called Vector whereas Set interface does not have any legacy class.

When to use List and when to use Set ?

If the requirement is to have only unique values then Set is your best bet as any implementation of Set maintains unique values only.

If there is a need to maintain the insertion order irrespective of the duplicity then List is a best option. Both the implementations of List interface – ArrayList and LinkedList sorts the elements in their insertion order.

See Example List :

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

public class Example {

public static void main(String[] args) {
ArrayList<String> arrlist = new ArrayList<String>();
       //add method for String ArrayList
arrlist.add("Item1");
arrlist.add("Item2");
arrlist.add("Item3");
arrlist.add("Item4");
arrlist.add("Item5");

System.out.println("Arraylist "+ arrlist);

List<String> linkedList = new LinkedList<String>();
linkedList.add("Item6-linkedlist");
linkedList.add("Item7-linkedlist");
System.out.println("\nLinkedList Elements: " +linkedList);

}
}

//result :
Arraylist [Item1, Item2, Item3, Item4, Item5]

LinkedList Elements: [Item6-linkedlist, Item7-linkedlist]


See Example Set :

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

public class ExampleSet {

public static void main(String[] args) {

Set<String> values = new HashSet<String>();
// add elements
values.add("Item1");
values.add("Item2");
values.add("Item3");
values.add("Item6");
values.add("Item4");
values.add("Item5");
System.out.println(" Values of Set = "+values);

// add set values to tree set
//treeset set order values
TreeSet<String> treesetValue = new TreeSet<String>(values);
System.out.println("Tree set  Values  = "+treesetValue);
}

}

//result ;


 Values of Set = [Item1, Item2, Item5, Item6, Item3, Item4]
Tree set  Values  = [Item1, Item2, Item3, Item4, Item5, Item6]








EmoticonEmoticon