Thursday, November 22, 2018
Home > java > HashMap in java example

HashMap in java example

HashMap in java example

HashMap in java example


HashMap class in java is implements on the Map interface by using Hashtable. It implements Map interface and inherits AbstractMap class.

The important feature of HashMap class are:

  • HashMap contains value based on key.
  • It contains only unique elements as key and value pair.
  • It may contains one null key & multiple null values.
  • it display data randomlly.

Hierarchy of HashMap:

HashMap class extends AbstractMap class and implements Map interface.


HashMap class Parameters

Parameters for HashMap class.

  • K: Key used to maintain sorting.
  • V: values which are mapped with key..

Constructors of Java HashMap class

Constructor

Description

HashMap()It is used to intialize HashMap.
HashMap(Map m)It is used to initializes the HashMap by using the elements of the given Map object m.
HashMap(int capacity)It is used to initializes the capacity of the HashMap to the given capacity.
HashMap(int capacity, float fillRatio)It is used to initialize both the capacity and fill ratio of the HashMap by using its arguments.

Methods of HashMap class

Method

Description

void clear()It is used to remove all data from map.
boolean containsKey(Object key)if it return true when this map contains a mapping for the given key.
boolean containsValue(Object value)It return true when map one or more keys to the given value.
boolean isEmpty()It return true if this map contains no data..
Object clone()It return a shallow copy of this HashMap instance: the keys & values themselves are not cloned.
Set entrySet()It return a collection of the mappings in the form of map.
Set keySet()It return a set view of the keys contained in map.
Object put(Object key, Object value)It is used to associate the given value with the given key in map.
int size()It return size of  map.
Collection values()It return a collection values contained in map.

Java HashMap Example

package demo;

import java.util.HashMap;
import java.util.Map;

public class Demo_HashMap
{
public static void main(String args[])
{
//Creating object of HashMap
HashMap<Integer,String> hm=new HashMap<Integer,String>();

//Adding element into HashMap by key:value pair
hm.put(1, "john");
hm.put(2, "Smith");
hm.put(3, "Rocky");
hm.put(4, "Michal");

//Retriving data from Hashset
for(Map.Entry<Integer,String> m:hm.entrySet())
System.out.println(m.getKey()+" "+m.getValue());
}
}

OUTPUT

1 john
2 Smith
3 Rocky
4 Michal

Java HashMap Example: remove()

package demo;

import java.util.HashMap;
import java.util.Map;

public class Demo_HashMap
{
public static void main(String args[])
{
//Creating object of HashMap
HashMap<Integer,String> hm=new HashMap<Integer,String>();

//Adding element into HashMap by key:value pair
hm.put(1, "john");
hm.put(2, "Smith");
hm.put(3, "Rocky");
hm.put(4, "Michal");

System.out.println(hm);

//Remove Element of key:3
hm.remove(3);
System.out.println(hm);
}
}

OUTPUT:

{1=john, 2=Smith, 3=Rocky, 4=Michal}
{1=john, 2=Smith, 4=Michal}

Difference between HashSet and HashMap

HashSet contains only values whereas HashMap contains entry(key and value).


Java HashMap Example: Student

package demo;

import java.util.HashMap;
import java.util.Map;

class Student
{
int rollNo;
String name;
int age;
Student(int rollNo,String name,int age)
{
this.rollNo=rollNo;
this.name=name;
this.age=age;
}
}
public class Demo_HashMap
{
public static void main(String args[])
{
Student s1=new Student(1,"Monty",13);
Student s2=new Student(2,"Jaha",14);
Student s3=new Student(3,"Clarke",15);
Student s4=new Student(4,"Jasper",16);
//Creating object of HashMap
HashMap<Integer,Student> hm=new HashMap<Integer,Student>();

//Adding element into HashMap by key:value pair
hm.put(1, s1);
hm.put(2, s2);
hm.put(4, s4);
hm.put(3, s3);

//Retrieving Data from HashMap
for(Map.Entry<Integer, Student> entry:hm.entrySet())
{
int key=entry.getKey();
Student s=entry.getValue();
System.out.println(key+" Detail:");
System.out.println("RollNo:"+s.rollNo+" Name:"+s.name+" Age:"+s.age);
}

}
}

OUTPUT

1 Detail:
RollNo:1 Name:Monty Age:13
2 Detail:
RollNo:2 Name:Jaha Age:14
3 Detail:
RollNo:3 Name:Clarke Age:15
4 Detail:
RollNo:4 Name:Jasper Age:16

Related Topices :-
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *