|
Synchronize EnumMap
The following example shows how to create a synchronized EnumMap and populate its entries.
|
package com.bethecoder.tutorials.utils.enums;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
public class EnumMapSynchronizedTest {
/**
* @param args
*/
public static void main(String[] args) {
//Create an empty fruit and cost map
Map<Fruit, Integer> enumMap =
Collections.synchronizedMap(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
|
|