Searching...
Friday, 22 November 2013

Java EnumMap Example

10:31

java.util.EnumMap is one of the specialized implementation of Map interface. It uses enum type as key and all keys must come from the same enum type. Here is one simple example of EnumMap.

Example

package com.pretech;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
public class EnumMapExample {
	public static void main(String[] args) {
		Map<Days, String> enumMap = new EnumMap<Days, String>(Days.class);
		enumMap.put(Days.SUNDAY, "Holiday");
		enumMap.put(Days.MONDAY, "Monday");
		enumMap.put(Days.TUESDAY, "Tuesday");
		enumMap.put(Days.WEDNESDAY, "Wednesday");
		Set<Days> keySet = enumMap.keySet();
		for (Days days : keySet) {
			System.out.println("Day is :" + enumMap.get(days));
		}
	}
}
enum Days {
	SUNDAY, MONDAY, TUESDAY, WEDNESDAY
}

Output



Day is :Holiday
Day is :Monday
Day is :Tuesday
Day is :Wednesday

0 comments: