JP Morgan Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
In Java a HashMap is a key value pair data structure that is not thread safe. A hashmap internally uses an array. This internal array contains Entry objects with fields that encapsulates the key, value and some other fields used internally by the hashmap. Hashmaps are generally used when you need to access data in constant time O(1) and order is not a concern when traversing the elements of the hashmap. Objects are stored in the hashmap using the key's hashcode to calculate the location in the hashmap. If an object already exist at a location where new object is to be inserted then a comparison is done on the two objects keys, hashvalue and equals is also called on the 2 keys to see if they are equal. If they are equal the old value is replaced with the new, if not the object is inserted into the hashmap.
HashMap is data structure which provides constant time performance for get and put operation. It uses the hashing mechanism to identify of the key object. Hashing means assigning the number to object. During put operation key is used to store the object in 'hashtable(normally array)' for get operation key(object) is calculated from the key object. which provides O(1) time lookup
HashMap is a data structure which stores data in the form of key-value pair.
A Hashmap is used implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
If we talk in terms of Space complexity and TimeComplexity.
It stores data the form of key-value, hence data can be inserted anywhere in memory location and based on key(which cannot be duplicated) value can be searched and deleted in constant time. Therefore,
In average case,
Space O(n)
Search, Insert, Delete O(1).
In Worst case,
Space O(n)
Search, Insert, Delete O(n).
In Java, Hashmap is a class used to implement Map(I).
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it 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.
In java hashmap works on Key and value pair.Hashmap consists of hashtable.
An instance of Hashtable 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 initial capacity is 16 and default load factor is 0.75.The threshold value come around 12.
When we call put method :
put<K,V>
then the hashcode is generated for the key and then it will call the hash function to get the index value in the range of hash bucket and value is getting stored at that particular generated index.If the hashcollision happens then equals come in to the picture and will check the hashcode and if it is same then will override the value or else create the linklist and shift the original value to next and stored in first.
The hashbucket contain the <K, V, hascode, next pointer>
When we call get method:
get<k> then the hashcode is generated for the key and then it will call the hash function to get the index value in the range of hash bucket.
If the entry is null then it will return null or else will check with K key with equals method and if found return the value.
Hashmap follows one thumb rule:
if two values are equals means the hashcode will be same but the vise-versa is not applicable.
The HashMap gives you an unsorted, unordered Map. When you
- Anonymous May 16, 2012need a Map and you don't care about the order (when you iterate through it), then
HashMap is the way to go; the other maps add a little more overhead. Where the
keys land in the Map is based on the key's hashcode, so, like HashSet, the more efficient
your hashCode() implementation, the better access performance you'll get.
HashMap allows one null key and multiple null values in a collection.