Net-informations.com
SiteMap  | About    

Java HashMap

In computer programming terms, a map is a set of associations between pairs of objects. Java HashMap is the basic implementation of Map interface. Java provides two data structures for hashtables: one is Hashtable and next one is HashMap. HashMap is similar to Hashtable with two exceptions: HashMap methods are unsynchronized and it allows null key and null values unlike Hashtable. Hashtable is synchronized and performs poorly in a single threaded environment. Because of that HashMap is generally preferred, unless you have to deal with threads and synchronization. HashMap is not a Thread safe collection and need synchronization handled properly.

how to Java hashmap

HashMap is a generic class, used for storing collection of the data in the form of key and value pairs and contains values based on the key. This HashMap implementation provides all sorts of the optional map operations, and permits null values and the null key. Moreover, it maintains no order.

Objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method.

How Java hashmap works?

HashMap works on the principle of Hashing. In simple, hashing is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. Hash function should return the same hash code each and every time, when function is applied on same or equal objects.

How Java hashmap works

HashMap has a number of "buckets" which it uses to store key-value pairs in. A bucket is used to store multiple key value pairs . In hash map, bucket used simple linked list to store objects. Each bucket has a unique number , that's what identifies the bucket. When you put(key, value) into the map, the hashmap will look at the hash code of the key, and store the pair in the bucket of which the identifier is the hash code of the key. For example, the hash code of the key is 512 the pair is stored in bucket number 512. if there is any collision ,HashMap uses LinkedList to store object. It is important to note that, one bucket can store more then one key-value pair.

When you lookup a value in the hashmap, by giving it a key (get(key)), the hashcode determines which bucket for the hashmap to check. It will first look at the hash code of the key that you gave. The hashmap will then look into the corresponding bucket , and then it will compare the key that you gave with the keys of all pairs in the bucket, by comparing them with equals(). If there is more than one object in the bucket then a linear search is done to find which item in the bucket equals the desired item by using the equals() method.

How can I add elements in Hashmap?

How to get Size of Java HashMap?

The size() method is used to return the number of key-value mappings in this map.

Output:

How to iterate over the elements in Hashmap?

There are several ways you can view items in Hashmap.

Use entrySet() to iterate through Map and need to access value and key:

Output:

Using for loop:

Output:

Using iterator and Map.Entry:

Output:

Using foreach and Map.Entry:

Output:

Using while loop:

Removing entries from a HashMap

The remove() method is used to remove the mapping for the specified key from this map if present.

Output:

Remove all values from Java HashMap

Output:

How to search a key in HashMap?

By using containsKey() method you can find out the key existence.

Output:

How to get Key from Value in HashMap?

Output:

The following Java program illustrates all the above mentioned method in a single program

Differences between HashMap and Hashtable

  1. Hashtable is synchronized, whereas HashMap is not synchronized. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform much better than synchronized ones. Synchronized means only a single thread can modify a hashtable at one point of time. Basically, that means any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

  2. The iterator in Hashmap is fail-fast iterator while the enumerator for Hashtable is not. According to Oracle Docs, if the Hashtable is structurally modified at any time after the iterator is created in any way except the iterator's own remove method , then the iterator will throw ConcurrentModification Exception. The word Structural modification means adding or removing elements from the Collection object such as hashmap or hashtable. Thus the enumerations returned by the Hashtable keys and elements methods are not fail-fast.

  3. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  4. In hashmap you can remove element while iterating, safely. where as it is not possible in hashtables.

  5. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order , you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

  6. Considering performance, Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.