Friday, 17 March 2017

What is HashMap in Java ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed about
What is Map Interface in Java ?
Here, we learn What is HashMap In Java.But before starting we must know what is Map Interface In Java.?
Class HashMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

All Implemented Interfaces:

Declaration of HashMap:
public class HashMap<K,V>

HashMap is a implementation class of Map Interface.It belongs to java.util.*; package .The HashMap class is roughly equivalent to Hashtable, except that HashMap is unsynchronized and permits nulls. 
This class does not guarantee that the order will remain constant over time. 
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.  
The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.  
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased,the default load factor is(.75). 
HashMap implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map means that thread can adds or deletes one or more mappings.To prevent this we can synchronized the Map using 

Map m = Collections.synchronizedMap(new HashMap(...));

Hashmap is an example of fail-fast.And This class is a member of the Java Collections Framework.

Constructor and Description :-

HashMap()
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).

HashMap(int initialCapacity, float loadFactor)
Constructs an empty HashMap with the specified initial capacity and load factor.

HashMap(Map<? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map.


No comments:

Post a Comment