9:31 pm - Saturday January 21, 2017

EnumMap for Mapping in Specfic Key using ContainsKey() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This Program is used EnumMap for Mapping in Specific Key using  ConstraintsKey() method

Example Program


package com.example;
import java.util.EnumMap;

public class EnumMapContainsKey { // create an enum
	public enum Numbers {
		ONE, TWO, THREE, FOUR, FIVE
	};

	public static void main(String[] args) {
		EnumMap<Numbers, String> map = new EnumMap<Numbers, String>(
				Numbers.class);
		// associate values in map
		map.put(Numbers.ONE, "1");
		map.put(Numbers.TWO, "2");
		map.put(Numbers.THREE, "3");
		map.put(Numbers.FOUR, "4");
		// print the whole map
		System.out.println(map); // check if map contains a mapping at specified
									// key
		boolean contains = map.containsKey(Numbers.ONE); // print the result
		System.out.println("Numbers.ONE has a mapping:" + contains);
		// check if map contains a mapping at another specified key
		contains = map.containsKey(Numbers.FIVE); // print the result
		System.out.println("Numbers.FIVE has a mapping:" + contains);
	}
}


 

.

Output:

{ONE=1, TWO=2, THREE=3, FOUR=4}
Numbers.ONE has a mapping:true
Numbers.FIVE has a mapping:false

 

Description

The java.util.EnumMap.containsKey(Object key) method determines if this map contains a mapping for the specified key.

Declaration

Following is the declaration for java.util.EnumMap.containsKey() method

public boolean containsKey(Object key)

Parameters

  • key– the key whose presence in this map is to be tested

Return Value

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

Exception

  • NA

 

BACK

Filed in: Java Examples, Z-A All Java Codes

Comments are closed.