What is difference between List, Set and Map

1. What is difference between List, Set and Map?

List

  • List is an ordered collection.
  • List allows duplicate elements.
  • List allows null elements and also store many null elements because it allows duplicate elements.

The following most useful classes implementing List interface

1. ArrayList  2. LinkedList  3. Vector

Set

  • Set is unordered collection.
  • Set does not allows duplicate elements.
  • Set just allow one null element and does not allow more than one null element.

The following most useful classes implementing Set interface

1. HashSet  2. LinkedHashSet 3. TreeSet

Map

  • Map is unordered collection
  • Map hold two objects per Entry e.g. Key and Value and it may contain dupliate values but not keys
  • Map allow only one null key and multiple null values.

The following most useful classes implementing Map interface

1. HashMap 2. LinkedHashMap 3. Hashtable  4. TreeMap

2. When to use List, Set and Map ?

List

  • Frequently accessing elements by using index. Its implementation ArrayList is faster access if you know the index
  • To store duplicate elements with order.

Set

  • To store unique data elements.

Map

  • To store data in the form of  Key and Value.

3. What is difference between ArrayList, Vector and LinkedList?

ArrayList

  • It is not thread-safe by default
  • It is not synchronized by default
  • Better performance in case of single thread
  • Collections.synchronizedList using this method we can make .synchronized ArrayList is equivalent of Vector.

Leave a Reply