Collections

Collection:

  • Consistent API
  • Reducing programmer efforts
  • Increase Speed and quality

Iterators:

  • Retrieve elements one by one
  • Types of Iterators
    • Enumeration
      • Interface used get elements of legacy collections (vector, hashtable)
      • Remove operation cannot be performed
      • Only forward direction is possible
    • Iterator
      • Can be used all collections types
      • Support remove operation
      • Cannot support adding new or replacing operation
      • Only forward direction
    • ListIterator
      • Only for List collection types
      • Bi directional
  • Any Iterator reference point to index just before index of first element
  • Cannot create object of them, as they are interfaces

Sets:

  • Cannot Store duplicates
  • Unordered collection
  • Union => addAll
  • Intersection => retainAll
  • Difference => removeAll
  • Types
    • HashSet
      • Value stored on basis of hash
      • Null Allowed | Time Complexity is O(1)
      • Uses hashmap to store values (constant value used to put in map)

List:

  • Can have duplicates
  • Ordered in form of insertion
  • Implemented by ArrayList, LinkedList, Stack and Vector
  • Index base access and saving
  • Searching by indexOf and lastIndexOf
  • Can also get sub list by indices
  • ArrayList
    • Dynamic Array
    • Extends AbstractList and Implements List
    • Allow random access
    • Primitives cannot be used

HashMap vs HashTable

  • HashMap is not thread safe, Hash Table is synchronised
  • HashMap allow one null key and multiple null value but hashtable do not allow null key or value
  • HashMap is implementation of hashing

Queue:

  • Priority Queue
    • To access data in queue based on priority
    • Does Not allow null
    • Elements are ordered by natural order or by comparator provided
    • Extends AbstractQueue, Abstract Collection
  • Dequeue
    • Double Queue
    • Allow addition and Removal from both ends

SortedMap:

  • Natural ordering or by comparator
  • Have methods like head tail sub etc
  • Navigational Map
    • Provide navigation method, provide way to create sub map (head & tail)
    • Methods
      • lowerKey(Object key) : Returns the greatest key strictly less than the given key, or if there is no such key.
      • floorKey(Object key) : Returns the greatest key less than or equal to the given key, or if there is no such key.
      • ceilingKey(Object key) : Returns the least key greater than or equal to the given key, or if there is no such key.
      • higherKey(Object key) : Returns the least key strictly greater than the given key, or if there is no such key.
      • descendingMap() : Returns a reverse order view of the mappings contained in this map.
      • headMap(object toKey, boolean inclusive) : Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
      • subMap(object fromKey, boolean fromInclusive, object toKey, boolean toInclusive) : Returns a view of the portion of this map whose keys range from fromKey to toKey.
      • tailMap(object fromKey, boolean inclusive) : Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
  • SortedSet and SortedMap
    • Null key or null value are not allowed
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: