Open the NetBeans IDE.
Create a new project.
Choose "Java" --> "Java Application".
Provide the project a name.
Write the following code.
Run the code.
(This process of working with NetBeans will be used in all the following examples.)
- clear( )
All the mappings from the map are removed by this method.
Syntax
publicvoid clear()
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(99,"Raj");
map.put(199,"Raja");
map.put(299,"Rajan");
System.out.println("INITIALLY: " + map);
map.clear();
System.out.println("\n");
System.out.println("ULTIMATELY: " + map);
}
}
Output
- clone( )
A shallow copy of the map instance is returned by this method.
Syntax
public Object clone()
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map1 =new HashMap();
HashMap map2 =new HashMap();
map1.put(99,"Raj");
map1.put(199,"Raja");
map1.put(299,"Rajan");
map2=(HashMap)map1.clone();
System.out.println("MAP ONE: " + map1);
System.out.println("\n");
System.out.println("MAP TWO(CLONED): " + map2);
}
}
Output
- containsKey(Object key)
In the identity HashMap, it is used to check that the given object reference is a key.
Syntax
public boolean containsKey(Object key)
It will return true if the given condition is satisfied.
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(99,"Raj");
map.put(199,"Raja");
map.put(299,"Rajan");
System.out.println("CHECK FOR KEY NINETY NINE : " + map.containsKey(99));
}
}
Output
- containsValue(Object value)
The identity HashMap is used to check that the given object is a value.
Syntax
public boolean containsValue(Object value)
Will return true if the map consists of one or more keys.
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(99,"Raj");
map.put(199,"Raja");
map.put(299,"Rajan");
System.out.println("CHECK FOR VALUE RAJAN : " +
map.containsValue("Rajan"));
}
}
Output
- get(Object key)
A value is returned to which the given key is mapped. A null is returned if there is no mapping.
Syntax
public A get(Object key)
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(99,"Raj");
map.put(199,"Raja");
map.put(299,"Rajan");
String val=(String)map.get(99);
System.out.println("VALUE FOR KEY NINETY NINE : " + val);
}
}
Output
-
isEmpty( )
It is checked by this method that the map consists of no key-value mappings.
Syntax
public boolean isEmpty()
It will return true if the given condition is satisfied.
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(7,"John");
map.put(8,"Johny");
map.put(9,"Jonty");
boolean val=map.isEmpty();
System.out.println("EMPTY: " + val);
}
}
Output
-
putAll( )
It is used to copy mappings from one map to the other.
Syntax
publicvoid putAll(Map m)
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map1 =new HashMap();
HashMap map2 =new HashMap();
map1.put(7,"John");
map1.put(8,"Johny");
map1.put(9,"Jonty");
System.out.println("MAP 1 : "+ map1);
map2.putAll(map1);
System.out.println("\n");
System.out.println("MAP 2 : "+ map2);
}
}
Output
-
remove( )
Mapping is removed for the specific key in the given map.
Syntax
public A remove(Object key)
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(7,"John");
map.put(8,"Johny");
map.put(9,"Jonty");
System.out.println("BEFORE: "+ map);
System.out.println("\n");
map.remove(8);
System.out.println("AFTER: "+ map);
}
}
Output
-
values( )
It will return all the values of the map.
Syntax
public Collection values()
Example
package demo;
import java.util.*;
publicclass Demo
{
public staticvoid main(String args[])
{
HashMap map =new HashMap();
map.put(7,"John");
map.put(8,"Johny");
map.put(9,"Jonty");
System.out.println("VALUES: "+ map.values());
}
}
Output