Showing posts with label java Collection framework. Show all posts
Showing posts with label java Collection framework. Show all posts

  • LinkedHashMap class is one of the class of map interface which extends HashMap class and implements Map interface.
  • It is combination of linked list and Hash table implementation of the Map interface and that's why it is called LinkedHashMap.
  • It is maintaining doubly-linked list insertion order in which keys were inserted into the map.
  • It allows to store null elements and optional Map operations are also available.
  • It is non synchronized implementation so if any thread modifies map structurally when map is accessed by multiple threads then it must be synchronized externally.

  • Is is pronounced as HashMap because It is hash table based implementation of map interface.
  • It extends AbstractMap class and implements Map interface.
  • It allows to store null key and null values.
  • It is non synchronized implementation so if multiple threads accessing it concurrently and any one of them modifies it then it must be synchronized externally.
  • It does not guarantee of maintaining the map order.

  • Hashtable Class implements Map, Cloneable and Serializable interfaces and also extends Dictionary class.
  • It maps keys to values in table format. Every key is linked to it's value.
  • In Hashtable, any object which is non null will be used as a key or it's value.
  • Initial capacity and load factor are two parameters of Hashtable that affect its performance.
  • Meaning of capacity is the number of buckets in Hashtable and Meaning of initial capacity is number of buckets in Hashtable at the time of its creation.
  • Load factor is parameter which measure how much full Hashtable is and when to increase it's capacity automatically.
  • In order to successfully retrieve and store object in Hashtable, the object which is used as key must implement the equals and the hashCode methods.

  • HashSet is a class of collection framework which extends AbstractSet class and implements the Set interface.
  • HasSet doesn't guarantee that elements order will remain same over the time and returned in any random order.
  • HasSet doesn't allow duplicate values. If you try to insert duplicate, It will overwrite.
  • HasSet allows to store null values.
  • HasSet implementation is not synchronized.
Hashset hierarchy in Java Collection

  • LinkedHashSet Class extends extends HashSet class and Implements Set interface in collection interface hierarchy.
  • LinkedHashSet implementation differs from HashSet as it maintains doubly-linked list running through all of its entries.
  • It maintains element's entries in set as per it's insertion order. So it will allow you insertion order iteration over the set. So when you iterate over LinkedHashSet, In which order it will return elements? Answer is As per it's insertion order.
  • So you will get same copy of elements with the order how they inserted in set. 
  • LinkedHashSetis not synchronized.
LinkedHashSet Class hierarchy

  • Set is one of the collection framework interface which extends root Collections interface.
  • Set collection can not contain duplicate elements.
  • Set interface has all methods which are inherited from Collection interface with special restriction of not allowing duplicate elements.
  • If two Set interface contains same elements then both are equal.
  • Set interface is implemented by LinkedHashSet, HashSet classes and extended by SortedSet interface which is implemented by TreeSet.
Java Set Interface hierarchy

  • Deque is one of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add and remove element from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque and usually pronounced as "deck".
  • Deque has different methods to perform insert, delete and examine the element in queue.
  • Each of these methods have two different forms. One will throw an exception if it fails during operation and other will return a special value like null or false.
java deque interface

  • PriorityQueue Class is one of the collection framework class which implements Queue, Serializable, Iterable and Collection interfaces.
  • It is using natural ordering to order then elements of PriorityQueue.Or elements will be ordered based on Comparator provided at queue construction time depending on which constructor is used.
  • PriorityQueue is based on a priority heap. Heap is tree based data structure where all the nodes of tree are in a specific order. Please read about Heap to learn how it works.
  • PriorityQueue does not allow non-comparable objects and it may throw ClassCastException if you will try to do so.
  • PriorityQueue does not allow null elements as well.
  • PriorityQueue implementation is not synchronized. So multiple threads should not access it concurrently.
Java PriorityQueue Class

  • ArrayList class is sub class of collection interface which implements to List interface.
  • ArrayList class provides resizable-array so it can grow automatically as per requirement which resolves fixed Array limitation where you have to pre-define the size of array.
  • It can hold null elements too.
  • It is not synchronized implementation so if multiple threads accessing it concurrently and any one from them modifies the entry then it must be synchronized externally.
  • Also it can hold duplicate elements.
Java ArrayList Class hierarchy