Difference between List, Set, Map (Java)

Send Us a Sign! (Contact Us!)

List, Set and Map are the interfaces which implements Collection interface. Here we will discuss difference between List, Set and Map in Java.

Difference between List, Set, Map (Java)

Duplicity

  • List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
  • Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
  • Map stored the elements as key & value pair. Map doesn’t allow duplicate keys while it allows duplicate values.

Null values

  • List allows any number of null values.
  • Set allows single null value at most.
  • Map can have single null key at most and any number of null values.

Order

  • List and all of its implementation classes maintains the insertion order.
  • Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
  • Similar to Set, Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order, the order in which the elements got added to the LinkedHashMap.

Commonly used classes

  • List: ArrayList, LinkedList etc.
  • Set: HashSet, LinkedHashSet, TreeSet, SortedSet etc.
  • Map: HashMap, TreeMap, WeakHashMap, LinkedHashMap, IdentityHashMap etc.

When to use List, Set and Map in Java

  • If you do not want to have duplicate values in the database then Set should be your first choice as all of its classes do not allow duplicates.
  • If there is a need of frequent search operations based on the index values then List (ArrayList) is a better choice.
  • If there is a need of maintaining the insertion order then also the List is a preferred collection interface.
  • If the requirement is to have the key & value mappings in the database then Map is your best bet.

List versus Set

  • 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).
  • 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.
  • List implementations: ArrayList, LinkedList etc.
  • Set implementations: HashSet, LinkedHashSet, TreeSet etc.
  • List allows any number of null values. Set can have only a single null value at most.
  • 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.
  • List interface has one legacy class called Vector whereas Set interface does not have any legacy class.

When to use Set and when to use List?

The usage is purely depends on the requirement:

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.

List Example

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

public class ListExample {

public static void main(String[] args) {
List al = new ArrayList<String>();
al.add(“Chaitanya”);
al.add(“Rahul”);
al.add(“Ajeet”);
System.out.println(“ArrayList Elements: “);
System.out.print(al);

List
ll = new LinkedList<String>();
ll.add(“Kevin”);
ll.add(“Peter”);
ll.add(“Kate”);
System.out.println(“\nLinkedList Elements: “);
System.out.print(ll);
}
}

Output:

ArrayList Elements:
[Chaitanya, Rahul, Ajeet]
LinkedList Elements:
[Kevin, Peter, Kate]

Set Example

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

public class SetExample {

public static void main(String args[]) {
int count[] = {11, 22, 33, 44, 55};
Set<Integer> hset = new HashSet<Integer>();
try{
for(int i = 0; i<4; i++){
hset.add(count[i]);
}
System.out.println(hset);

TreeSet treeset = new TreeSet<Integer>(hset);
System.out.println(“The sorted list is:”);
System.out.println(treeset);
}
catch(Exception e){
e.printStackTrace();
}
}
}

Output:

[33, 22, 11, 44]
The sorted list is:
[11, 22, 33, 44]