java list vs map code example
Example 1: list vs map
LIST: Can store duplicate values,
Keeps the insertion order.
It allows multiple null values,
Also we can read a certain value by index.
- ArrayList not syncronized, array based class
- LinkedList not synchronized, doubly linked
- Vector is synchronized, thread safe
MAP : is a (key-value format)
and keys are always unique,
and value can be duplicated.
- HashTable don't have null key, sychronized(thread-safe)
- LinkedHashMap can have null key, keeps order
- HasHMap can have null key, order is not guaranteed
- TreeMap doesn't have null key and keys are sorted
Example 2: difference between list vs set vs map in java
Set is unordered
List is ordered collection,
List allow duplicate elements
Set does not allow duplicates.
List can have multiple null elements
Set will allow only one null element.
The List can contain duplicate elements
Set includes unique items.