Tuesday, May 18, 2021

Java Collection: Hierarchy and Interfaces Summary

 


Collection

  1. Parent interface of collection framework which implements ‘Iterable’ interface

  2. Defines most common methods applicable for all collection objects

  3. This interface is not implemented by any concrete class

List

  1. Child interface in Collection interface

  2. To be used when duplicate elements are required and insertion order
    to be preserved

Set

  1. Child interface in Collection interface

  2. Set interface does NOT define any new methods; methods from Collection are used

  3. To be used when duplicate elements are NOT allowed and insertion order
    is not required

Sorted Set

  1. Child interface of Set interface

  2. Stores elements in a specific sorting order

  3. Duplicates are not allowed

  4. Methods: first, last, headSet, tailSet, subSet, comparator => check TreeSet

Navigable Set

  1. Child interface of Sorted Set interface

  2. Provides various methods for navigation 

  3. Is implemented by TreeSet

  4. Methods

    1. floor(e): returns highest elements which is <= e

    2. lower(e): returns highest elements which is < e

    3. ceil(e): returns lowest elements which is >= e

    4. higher(e): returns lowest elements which is > e

    5. pollFirst(): remove & return first element

    6. pollLast(): remove & return last element

    7. descendingSet(): returns NavigableSet in reverse order

Queue

  1. It is child interface of Collection interface

  2. Queue guarantees FIFO (First In First Out) behavior 

  3. ArrayList also preserves the insertion order and can be used in place of Queue,
    but FIFO behavior cannot be guaranteed (due to possibility of adding or reading
    elements using index at any positions)

  4. Methods

    1. Offer: to add an element in the queue

    2. Poll: to remove element, if queue is empty, returns NULL

    3. Remove: to remove element, if queue is empty,
      raises NoSuchElementException

    4. Peek: to return an element, if queue is empty, returns NULL

    5. Element : to return an element, if queue is empty,
      raises NoSuchElementException

Map

  1. NOT a child interface of Collection interface

  2. Stores records as key-value pairs

  3. Duplicate keys not allowed, but duplicate values are allowed

  4. Put method returns ‘object’ which is overwritten (i.e. value which is replaced)
    by current key, if the current key is not duplicate, then it returns null

Sorted Map

  1. Child interface of Map interface

  2. Stores records in a sorted order of keys

Navigable Map

  1. Child interface of Sorted Map interface

  2. Defines several utility methods for navigation purpose

  3. Is implemented by TreeMap

  4. Methods

    1. floorKey(e): returns highest key which is <= e

    2. lowerKey(e): returns highest key which is < e

    3. ceilingKey(e): returns lowest key which is >= e

    4. higherKey(e): returns lowest key which is > e

    5. pollFirstEntry(): returns & removes first Entry in the Map

    6. pollLastEntry(): : returns & removes last Entry in the Map

    7. descendingMap(): : returns reversed Map

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...