|
Create EnumMap
The following example shows how to create an EnumMap and populate its entries.
|
package com.bethecoder.tutorials.utils.enums;
import java.util.EnumMap;
public class EnumMapTest {
/**
* @param args
*/
public static void main(String[] args) {
//Create an empty fruit and cost map
EnumMap<Fruit, Integer> enumMap = new EnumMap<Fruit, Integer>(Fruit.class);
//Populate map with fruit and its cost
for (int i = 0 ; i < Fruit.values().length ; i ++) {
enumMap.put(Fruit.values()[i], (i+1) * 100);
}
System.out.println("Fruit Map : " + enumMap);
System.out.println("Fruit Map Size : " + enumMap.size());
}
}
|
| |
|
package com.bethecoder.tutorials.utils.enums;
public enum Fruit {
APPLE,
ORANGE,
BANANA,
CHERRY,
MANGO
}
|
| |
It gives the following output,
Fruit Map : {APPLE=100, ORANGE=200, BANANA=300, CHERRY=400, MANGO=500}
Fruit Map Size : 5
|
|