tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collection > EnumMap > Synchronize EnumMap

Synchronize EnumMap 

The following example shows how to create a synchronized EnumMap and populate its entries.

File Name  :  
com/bethecoder/tutorials/utils/enums/EnumMapSynchronizedTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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 = ; i < Fruit.values().length ; i ++) {
      enumMap.put(Fruit.values()[i](i+1100);
    }
    
    System.out.println("Fruit Map : " + enumMap);
    System.out.println("Fruit Map Size : " + enumMap.size());
  }

}
   


File Name  :  
com/bethecoder/tutorials/utils/enums/Fruit.java 
   
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



 
  


  
bl  br