Collections

Java Enumset Methods Code With Examples

Table of Contents

Program

import java.util.HashMap;

public class HashMapWithMethods 
{
	public static void main(String[] args) 
	{
		HashMap<Integer, String> map1 = new HashMap<Integer, String>();
		HashMap<Integer, String> map2 = new HashMap<Integer, String>();
		map1.put(1, "Class Is a");
		map1.put(2, "Collection of ");
		map1.put(3, "Objects.");
		map1.put(4, "Object is a");
		map1.put(5, "Collection of");
		map1.put(6, "Data and Function");
		System.out.println("Elements :" + map1);
		System.out.println("");
		
                //clone()
                map2 = (HashMap) map1.clone();
		System.out.println("1. Map2 Clone :" + map2);
		System.out.println("");

                //containsKey​(Object key)
		System.out.println("2. Contains Key :" + map1.containsKey(4));
		System.out.println("");
		
                //containsValue​(Object value)
                System.out.println("3. Check Contain Value 'Collection of' exists :" + map1.containsValue("Collection of"));
		System.out.println("");
		
                //clear()
                map1.clear();
		System.out.println("4. Map Cleared : " + map1);
		System.out.println("");
		System.out.println("5. Int Size :-");

                //size()
		System.out.println("Map1 :" + map1.size());
		System.out.println("Map2 :" + map2.size());

	}
}

Output

 	Elements :{1=Class Is a, 2=Collection of , 3=Objects., 4=Object is a, 5=Collection of, 6=Data and Function}

	1. Map2 Clone :{1=Class Is a, 2=Collection of , 3=Objects., 4=Object is a, 5=Collection of, 6=Data and Function}

	2. Contains Key :true

	3. Check Contain Value 'Collection of' exists :true

	4. Map Cleared : {}

	5. Int Size :-
	Map1 :0
	Map2 :6 

Description

public Object clone()

Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Overrides:

clone in class AbstractMap<K,V>

Returns:

a shallow copy of this map

Parameters:

key – the key with which the specified value is to be associated
value – the value to be associated with the specified key

Returns:

the previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null wit6h the specified key.)

Throws:

cloneable

public boolean containsKey​(Object key)

Returns true if this map contains a mapping for the specified key.

Specified by:

containsKey in interface Map<K>,V>

Overrides:

containsKey in class AbstractMap<K>,V>

Parameters:

key – The key whose presence in this map is to be tested

Returns:

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

public boolean containsValue​(Object value)

Returns true if this map maps one or more keys to the specified value.

Specified by:

containsValue in interface Map<K>,V>

Overrides:

containsValue in class AbstractMap<K>,V>

Parameters:

value – value whose presence in this map is to e tested

Returns:

true if this map maps one or more keys to the specified value

public void clear()

Removes all of the mappings from this map. The map will be empty after
this call returns.

Specified by:

clear in interface Map<K>,V>

Overrides:

clear in class AbstractMap<K>,V>

public int size()

Returns the number of key-value mappings in this map.

Specified by:

size in interface Map<K>,V>

Overrides:

size in class AbstractMap<K>,V>

Returns:

the number of key-value mappings in this map