how to work with hash map in java

14
Apex T. G. India Pvt. Ltd Working With HashMap Collection Framework

Upload: apex-tgi

Post on 28-Jun-2015

269 views

Category:

Technology


4 download

DESCRIPTION

HashMap is HashTable based implementation of Map. This is the reason why interviewer always ask for difference between HashMap and HashTable. HashMap is mostly equals to HashTable except below two differences.

TRANSCRIPT

Page 1: How to Work with Hash Map in Java

Apex T. G. India Pvt. Ltd

Working With HashMap

Collection Framework

Page 2: How to Work with Hash Map in Java

1

Working With HashMap HashMap is HashTable based implementation of Map. This is

the reason why interviewer always ask for difference

between HashMap and HashTable. HashMap is mostly

equals to HashTable except below two differences.

HashMap is unsynchronized while HashTable is

synchronised.

HashMap permits null while HashTable doesn’t.

Page 3: How to Work with Hash Map in Java

1

Working With HashMap DEFAULT_INITIAL_CAPACITY  : Default Initial Capacity(2

power n). Number of element HashMap can contain.

MAXIMUM_CAPACITY  : Maximum Capacity of HashMap (2

power n).

LOADFACTOR: Defines threshold of HashMap. When re-

sizing will occur of HashMap.

Page 4: How to Work with Hash Map in Java

1

Working With HashMap DEFAULT_LOAD_FACTOR  : Will be used when no load factor

is defined in constructor of HashMap.

SIZE : Number of key-value pair mapping, HashMap

contains.

When there is no parameter defined while creating

HashMap default Initial Capacity(16) and Default load

factor(0.75) will be used.

Page 5: How to Work with Hash Map in Java

1

Working With HashMap HashMap can contain up to 16 element and resizing of

HashMap will occur when 13th element will be inserted.

This is because load factor is 75%(.75) and this threshold

will be crossed when you add 13th element(12+1).

We can also provide initial capacity and load Factor. But

initial capacity can not be more than maximum capacity (2

power 30) and load factor can not be zero or negative

number.

Page 6: How to Work with Hash Map in Java

1

Working With HashMap In order to add any element we need to provide 2 thing, key

and value.

Key : key with which specified value will be

associated. null is allowed.

Value : value to be associated with specified key.

First HashMap will generate hashcode for given key and

then check if there is any value already associated with

given key or not.

Page 7: How to Work with Hash Map in Java

1

Working With HashMap If there is any value already associated with given key then it

will return already associated value. Else it will add value in

HashMap with provided key.

HashMap doesn’t give any Guarantee in order of elements in

Map(Means Order can change over time).

HashMap provide Constant time performance

for get & set operation(If proper Hashing algorithm is used).

Page 8: How to Work with Hash Map in Java

1

Working With HashMap Time requires to Iterate collection is proportional to

“Capacity“(Elements it can hold) & Size(Elements it is

holding currently) of HashMap.

In case iteration performance is more important then it is

advisable to not set initial capacity too high and load factor

too low. As performance is directly proportional to Initial

Capacity and load Factor.

capacity is the number of buckets in the hash table.

Page 9: How to Work with Hash Map in Java

1

Working With HashMap initial capacity(Default Value is 16) is simply the capacity at the time

the hash table is created.

The load factor(Default value .75) is a measure of how full the hash

table is allowed to get before its capacity is automatically increased.

When the number of entries in the hash table exceeds the product of

the load factor and the current capacity, the hash table

is rehashed (that is, internal data structures are rebuilt) .

“Collections.synchronizedMap()” method is used to make

Map synchronised.

Page 10: How to Work with Hash Map in Java

1

Working With HashMap Iterators returned by HashMap class is “fail-fast“.

HashMap is backed by an Array(Key) and LinkedList(Value).

HashMap uses hashcode(Using Key) to identify exact

location where object should be placed or retrieved in

HashMap.

In the end HashCode return the exact location(Index) in

backing array.

Page 11: How to Work with Hash Map in Java

1

Working With HashMap Backed Array have a fixed size. So whenever Array is

full(Number of keys in this map reaches its threshold). A

new Array with new capacity will be created and all element

will be added to this new Array.

HashCode will be used in both cases(Adding  and Retrieving

Object) while equals() method may or may not be used in

any case.

Page 12: How to Work with Hash Map in Java

1

Working With HashMap Best candidate for Key in HashMap would be an Immutable

Class with properly implement Equals and Hashcode

method(Example: String Class and Wrappers Class).

The better hashcode and equals method implementation is

better performance of HashMap would be.

In such way String and Wrapper classes of all Primitives will

be great candidate for keys in HashMap.

Page 13: How to Work with Hash Map in Java

1

Contact us

Page 14: How to Work with Hash Map in Java

Thanks

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on JAVA