Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 7 June 2017

Java HashMap

 

HashMap Class in Java

Java HashMap is a class in java collection framework. Java HashMap class implements Map interface and extends AbstractMap class.

Java HashMap class stores key & value pairs, In other word it stores the value on the basis of key.

Java HashMap class is similar to the Hashtable class except that it is non-synchronized.


Some Important Points About Java HashMap Class are :

  • Java HashMap class implements Map interface and extends(inherit) AbstractMap class.
  • HashMap class stores key & value pairs.
  • HashMap class contains only unique elements.
  • HashMap class are  not synchronized.
  • HashMap class doesn't maintains insertion order of an elements.
  • HashMap class may have one null key and multiple null values.
  • We can retrieve elements from HashMap by using foreach loop, Iterator interface and ListIterator interface.

Hierarchy of Java HashMap Class

Java HashMap
In the above hierarchy HashMap class extends AbstractMap class and AbstractMap class implements Map interface.


Constructors of HashMap Class

There are some constructor of hash map class and their description.

1) HashMap()

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

2) HashMap(Map m)

It is used to initializes the hash map by using the element of the given Map object m.

3) HashMap(int initialCapacity)

It construct an empty HashMap with the specified initial capacity and the default load factor(0.75).


4) HashMap(int initialCapacity, float loadFactor)

Create an empty HashMap with the specified initial capacity and load factor.


Methods of Java HashMap Class 

There are certain methods of hash map class.

1) void clear()

This method removes all of the mapping from this map.

2) Object clone()

This method returns a shallow copy of the HashMap instance: the key and value  themselves are not cloned.

3) boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

4) boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

5) boolean isEmpty()

If this map contains no key and value, returns true.

6) Set keySet()

This method returns a Set view of the key contained in this map.

7) Set entrySet()

Returns a Set view of the mappings contained in this map.

8) int size()

This method returns the number of key-value mappings in this map.

9) Collection value()

This method returns a collection view of the values contained in this map.

10) void putAll(Map m)

Copies all the elements of a map to the another specified map.


Java HashMap Example

This is simple Java HashMap example , Where we will store the value on the basis of key.

import java.util.*;
class HashMapExample
{
public static void main(String args[])
{
//creating HashMap object
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(100, "sachin");
hm.put(101, "virat");
hm.put(102, "sourav");
hm.put(103, " vvs");
hm.put(101, "sehwag");

//traversing elements by using for each loop
for(Map.Entry me : hm.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}
}

output : 100 sachin
              101 sehwag
              103 vvs
              102 sourav

Q. What is difference between HashSet and HashMap class ?

Ans. HashSet : HashSet class implements Set Interface and it stores only single value i.e there is no key & value combination.

for example : hs.add("javatutorial95");

HashMap : HashMap class implements Map interface and it stores the value on the basis of key. There is key & value combination.

for example : hm.put(1, "javatutorial95");





Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate