Showing posts with label HashMap. Show all posts
Showing posts with label HashMap. Show all posts

Saturday, July 27, 2013

Java : How HashMap stores and returns element

Hashing technique is useful in searching exact data in very short time.
In Java Collection Framework, Hashmap class provides functionality to store data using hashing techniques.
Here is how HashMap works internally.

Following main parameters are used to store data in HashMap:
  • Internal static class Entry which contains following variables
    • Key
    • Value
    • Next element
    • Hash value
  • Array of Entry objects (Bucket)
  • Threshold (Initial capacity * load factor)


While putting element in HashMap:
·         If key is null, it allows putting single value against null key. If you are trying to add another value against null key, it returns existing value in HashMap against null key and stores new value against null key.
o   Entry Object is stored at Bucket location 0. Hash value is also zero for null key.


·         How Value is stored against key in HashMap:
  •       Internal hash function is used to calculate hash value for hashcode of key. Hashcode of key is passed to hash function to get hash value.
  • Bucket index is found based on hash value and length of bucket. (Bucket is array which is used to store data. Data is Entry objects which contains key, value, next object and hash values). Bucket index = hash value & (bucket size-1). Bitwise and operator is used to find out index value.
  • Iterator is used to iterate over Entry objects at given index (if exists) to check if same key is already exists by comparing hash value and object equality using equals function. If same key is found, new value is stored against key and old value is returned.

Tuesday, June 25, 2013

Java : General purpose Implementation Classes in Collection Framework


Collection framework consists of general purpose implementation of Set, Map, List interfaces.
Details of Interfaces in Collection framework is available at Collection Framework Interfaces
Main characteristics of General purpose implementation in Collection framework is described as below.

HashSet:
  • Extends AbstractSet, implements Set, Cloneable, Serializable.
  • Implements Set interface backed by HashMap instance.
  • Null element is allowed.
  • Unordered collection.
  • Default initial capacity of 16 and load factor of 0.75.
  • Constant time performances for basic operations (add, remove, size, contains).
  • Iterating over set requires time proportional to number of elements in Set and capacity of backing HashMap instance.
  • Not synchronized.
  • Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.

TreeSet:
  •  Extends AbstractSet, implements NavigableSet, Cloneable, Serializable
  • Implementation is based on TreeMap.
  • Elements are ordered by natural order or by Comparable provided at the time of creation of Set.
  • Not synchronized.
  • Null is not allowed.
  • Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
  • There are methods available to get subset of existing Set. Subset is backed by original Set, so any addition of element within range to set will be reflected to Subset and vice-versa.
    • subset(fromElement, toElement) - fromElement is inclusive and toElement is excluded
    • subSet(fromElement,  boolean fromInclusive, toElement, boolean toInclusive) –using Boolean fromElement and toElement can be made inclusive or excluded.

ArrayList:
  • Extends AbstractList, implements List, Cloneable, Serializable, RandomAccess.
  • Random access of elements is fast compared to LinkedList.
  • Element are stored in sequence in array.
  • Null is allowed.
  • Not synchronized.
  • Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
  • ArrayList has capacity which is size of array to store objects. ensureCapacity  method is used to make sure that size of ArrayList is appropriate.
  • Default initial capacity is 10.


LinkedList:
  • Extends AbstractSequentialList, implements List, Deque, Cloneable, Serializable.
  • Null is allowed.
  • Methods available to add, remove, get element at the beginning and end of the list.
  • Not synchronized.
  • Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.

  
HashMap:
  • Extends AbstractMap, Implements Map, Serializable, Cloneable.
  • HashMap allows one null key
  • Not synchronized.
  • Iterator returned by this class is fail-fast. If set is modified after iterator is created, iterator throws ConcurrentModificationException.
  • HashMap has two parameters – Initial capacity and load factor which affects performance.
  • Default initial capacity 16
  • Default load factor 0.75
  • Rehashing is done when capacity/load factor is more than number of elements in HashMap.

Thursday, June 13, 2013

Java: Storing null key in HashMap

HashMap is useful in searching exact data in short time.
HashMap allows only one null key. Let's see how it stores null key internally.

If key is null, HashMap allows putting single value against null key. 
If you are trying to add another value against null key, it returns existing value for null key and stores new value against null key.
Internally HashMap maintains array of Entry class(internal class of HashMap which is used to store data) which is also called as Bucket.
Entry class contains key, value, nextElement, hash-value variables.

Data with null key is stored at Bucket location 0(array index 0 of Entry array). Hash value is also zero for null key.

Example:
HashMap h = new HashMap();  You can also use HashMap instead of HashMap.
h.put(null, "A");         -- Value "A" is stored against null key
h.put(null, "B");         -- Value "B" is stored against null key and old value "A" is returned as return type of put function is V i.e. value object
h.put(null, "C");         -- Value "C" is stored against null key and old value "B" is returned