Collection Interface

Collection Interface

collectionsschema

 

List Set Map
provides ordered and indexed collection which may contain duplicates. provides an unordered collection of unique objects, i.e. Set doesn’t allow duplicates An object that maps keys to values. A map cannot contain duplicate keys
https://docs.oracle.com/javase/tutorial/collections/implementations/list.html https://docs.oracle.com/javase/tutorial/collections/implementations/set.html https://docs.oracle.com/javase/tutorial/collections/implementations/map.html

1.1 Vector, ArrayList and LinkedList Comparison

Operation Vector ArrayList LinkedList
Positional Access Constant Time – O(1) Constant Time – O(1) Linear time – O(n)
Deletion Linear time – O(n) Linear time – O(n) Constant Time – O(1)
Insertion Linear time – O(n) Linear time – O(n) Constant Time – O(1)
Order Same as inserted Same as inserted Same as inserted
Implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>

 

1.2 HashSet, TreeSet and LinkedHashSet Comparison

Operation HashSet TreeSet LinkedHashSet
Ordering/Sorting N/A Available(Comparable or Comparator) Insertion Order
Duplication N/A N/A N/A
Speed Fastest Slower Faster(nearly as fast as HashSet)
Insertion, Removal and Contains Logarithmic – O(log(n)) Constant Time – O(1) Logarithmic – O(log(n))
Nullable Allows Disallows Allows
Comparison By equals method By compareTo method By equals method
Implements Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E> Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>, SortedSet<E> Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

mapscollectionsuml

1.3 HashMap, Hashtable, TreeMap and LinkedHashMap Comparison

Operation HashMap Hashtable TreeMap LinkedHashMap
Ordering/Sorting N/A N/A Key ordered(comparator) Insertion Order
Speed Fastest Slower(synchronized) Slower as the fastest as HashMap
Insertion, Removal and Contains Constant Time – O(1) Constant Time – O(1) Constant Time – O(1) Logarithmic – O(log(n))
Nullable It allows to store one null key and many null values i.e. many keys can have null value. It does not allow to store null key or null value. Any attempt to store null key or value throws runtimeException(NullPointerException) It allows to store one null key and many null values i.e. any key can have null value. It does not allow to store null key but allow many null values. Any attempt to store null key throws runtimeException (NullPointerException).
Implements Serializable, Cloneable, Map<K,V>Set<E> Serializable, Cloneable, Map<K,V> Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V> Serializable, Cloneable, Map<K,V>

Leave a Reply

Your email address will not be published. Required fields are marked *