What is the difference between List, Set and Map in Java?
UPDATED: 13 October 2016
Tags:
Collection
,
Interview
List interface
An ordered collection (also known as a sequence).
Characteristics
- Element added from 0th index, 1st, 2nd, ... nth (Sequentially).
- List allows to insert/update/read element at specific index.
- List allows duplicate values.
- It maintains insertion position (1st point).
- It allows null value.
ArrayList is one of the popular implementation of List used by programmers.
Examples
- How to Initialize List in declaration?
- How List.add() and List.addAll() works?
- How to sort List of Bean in Java?
- How to remove all elements from List except specific?
- How to convert List of data to Set of data and vice versa in Java?
- How to reverse List in Java Collection Framework?
- Collection Framework examples in Java 8
Set interface
Collection which don't allows duplicate values.
Characteristics
- Unlike List, Set will not allow index based insert/update/read.
- Set doesn't allow duplicate. e1 and e2 such that e1.equals(e2)
- It doesn't maintain insertion position (unordered collection). However you can maintain order using LinkedHashSet.
- It allows null value.
HashSet is one of the popular implementation of Set used by programmers.
Examples
- How to Initialize Set in declaration?
- How to convert List of data to Set of data and vice versa in Java?
- Collection Framework examples in Java 8
Map interface
Collection to hold [KEY, VALUE] data.
Characteristics
- Unlike List, Map will not allow index based insert/update/read.
- Map doesn't allow duplicate KEY.
- It doesn't maintain insertion position (unordered collection). However you can maintain order using LinkedHashMap.
- It allows one null KEY and n number of null VALUE.
HashMap us one of the popular implementation of Map used by programmers.
Collection Framework examples in Java 8
Tags:
Collection
,
Interview
0 comments :