• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

HashMap




Map interface implemented class:
Map interface is implemented in the following predefined classes
1.Hashmap
2.HashTable

All the implemented classes of Map interface allows elements in the form of key value pair.
Note : Initially 16 memory locations are created for both Map interface and Set interface implemented classes.Once the element is stored in 16th locations 75% of next memory locations will be added.
HashMap Class:
It is a predefined class in java.util package.It accepts the data in the form of key value pair, key always accepts unique element where as value allows duplicate elements.
HashMap class objects not synchronized that means multiple threads are allowed to work on same reference.
HashMap contains following methods.
HashMap<wrapperclass1,wrapperclass2> objref=new HashMap<wrapperclass1,wrapperclass2>();

wrapperclass1 is key
wrapperclass2 is value
 
Hashmap put Method
Which can be used to add a new element along with key element.
objref.put(key,value);
import java.util.*;
class HashMapPut
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
}
}

class MAin
{
public static void main(String aargs[])
{
HashMapPut d=new HashMapPut();
d.list();
}
}
 
Hashmap get Method:
It is used to get a element based on key. objref.get(key);
import java.util.*;
class HashMapGet
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.get(2));
}
}

class Main
{
public static void main(String aargs[])
{
HashMapGet d=new HashMapGet();
d.list();
}
}

Hashmap isEmpty Method:
Which return true if the map is empty otherwise it returns false
import java.util.*;
class HashMapIsEmpty
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.isEmpty());
}
}

class Main
{
public static void main(String aargs[])
{
HashMapIsEmpty d=new HashMapIsEmpty();
d.list();
}
}
 

Hashmap remove Method :
It is used to remove an element based on key value. objref.remove(key);
import java.util.*;
class HashMapIsRemove
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println("The Removing Element is "+al.remove(4));
}
}

class Main
{
public static void main(String aargs[])
{
HashMapIsRemove d=new HashMapIsRemove();
d.list();
}
}

Hashmap size Method:
returns number of key value mappings in the map.
import java.util.*;
class HashMapSize
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.size());
}
}

class Main
{
public static void main(String aargs[])
{
HashMapSize d=new HashMapSize();
d.list();
}
}
 

Hashmap keySet Method:
Which can be used to gets all the key elements which are existing in the specified map.
import java.util.*;
class HashMapKeySet
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.keySet());
}
}

class Main
{
public static void main(String aargs[])
{
HashMapKeySet d=new HashMapKeySet();
d.list();
}
}
 
Hashmap Values Method:
Which returns all the set of values which are available in the map.
import java.util.*;
class HashMapValue
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.values());
}
}

class Main
{
public static void main(String aargs[])
{
HashMapValue d=new HashMapValue();
d.list();
}
}
 
Hashmap Clear Method:
Which is used to remove all the key value pair from the specified map.
import java.util.*;
class HashMapClear
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
al.clear();
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
HashMapClear d=new HashMapClear();
d.list();
}
}
 
Hashmap ContainsKey Method:
Which returns true if the map contains given key element objref.containsKey(key);
import java.util.*;
class HashMapContainsKey
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);

System.out.println(al.containsKey(2));

}
}

class Main
{
public static void main(String aargs[])
{
HashMapContainsKey d=new HashMapContainsKey();
d.list();
}
}
 
Hashmap ContainsValue Method:
Returns true if the given value is mapped by any key element otherwise returns false.
import java.util.*;
class HashMapContainsValue
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);

System.out.println(al.containsValue("chennai"));
}
}

class Main
{
public static void main(String aargs[])
{
HashMapContainsValue d=new HashMapContainsValue();
d.list();
}
}

Hashmap PutAll Method :
Which is used to copy all the key value pair into another Map.
import java.util.*;
class HashMapPutAll
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
HashMap<Integer,String> al2=new HashMap<Integer,String>();
al2.putAll(al);
System.out.println(al2);
}
}

class Main
{
public static void main(String aargs[])
{
HashMapPutAll d=new HashMapPutAll();
d.list();
}
}

Hashmap Clone Method :
Returns a duplicate copy from current map memory.
import java.util.*;
class HashMapClone
{
void list()
{
HashMap<Integer,String> al=new HashMap<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
HashMap<Integer,String> al2;
al2=(HashMap)al.clone();
System.out.println(al2);

}
}

class Main
{
public static void main(String aargs[])
{
HashMapClone d=new HashMapClone();
d.list();
}
}