This post will discuss the difference between the List, Set, and Map interface in Java.

The List, Set and Map interfaces are significant members of the Java Collections Framework. This post will point out the major differences between them.

1. Ordering

List represents an ordered sequence in Java whose elements are accessible by index.

Set represents a distinct collection of elements in Java which can be either ordered or unordered, depending on the implementation. For example, HashSet implementation is unordered, LinkedHashSet implementation is ordered, and TreeSet implementation is ordered by natural order or provided comparator.

Map represents the mapping of the key to values in Java. The ordering in Map is also implementation-specific. For example, the TreeMap class is ordered while the HashMap class is not.

2. Duplicates

Another notable difference between List, Set and Map interface is that a List can have duplicate elements, a set contains only distinct elements, and a Map doesn’t permit duplicate keys, i.e., each key can map to at most one value.

3. Null values

List allows any number of null values while a set contains at most one null element. A Map typically allows null as a key and value, but some implementations prohibit null keys and values.

4. Implementing classes

Few of the most popular implementing classes of the List interface are ArrayList and LinkedList while that of Set interface includes HashSet, TreeSet and LinkedHashSet. Map interface offers the HashMap, TreeMap, and LinkedHashMap classes.

5. When to use List, Set and Map?

A List can be used when the insertion order of elements needs to be maintained. We can use a set if we need to maintain a collection that contains no duplicates. And Map` when data is key-value pairs and need fast retrieval of value based on some key.

That’s all about the differences between the List, Set, and Map interface in Java.

 
References:

1. List Javadoc SE 9
2. Set Javadoc SE 9
3. Map Javadoc SE 9