0

I am working on EnumMap and i want to why know it is fail safe though none of the methods of EnumMap is synchronized.

I am not taking iterator on key set but using for each loop

 public class JavaEnumMapExample {

  public enum MealType {
    BREAKFAST, LUNCH, SNACK, DINNER
}

public static void main(String[] args) {

    Map<MealType, String> myMealMap = new EnumMap<MealType, String>(MealType.class);

    // populate the map
    myMealMap.put(MealType.BREAKFAST, "Enjoy Milk and Eggs for breakfast!");
    myMealMap.put(MealType.LUNCH, "Enjoy Chicken, Rice and bread for Lunch!");
    myMealMap.put(MealType.SNACK, "How about an apple for the evening snack!");
    myMealMap.put(MealType.DINNER, "Keep the dinner light, lets have some salad!");

    System.out
            .println("Welcome to meal planner, we have suggestions for following meals : ");

    // print all the keys of enum map in sorted order
    System.out.println(myMealMap.keySet());

    // We can get the value from enumType
    System.out.println(" Q: What should I have for lunch? ");
    System.out.println(" A: " + myMealMap.get(MealType.LUNCH));

    System.out.println(" Q: What should I have for snack? ");
    System.out.println(" A: " + myMealMap.get(MealType.SNACK));

    System.out.println(" Q: What should I have for dinner? ");
    System.out.println(" A: " + myMealMap.get(MealType.DINNER));

    // Iterate over enumMap
    for (MealType mealType : myMealMap.keySet()) {
        System.out.println(myMealMap.get(mealType));
    }
    System.out.println("*** Checking for concurrent modification exception! ***");
    // Does not throw Concurrent modification Exception in enumMap
    for (MealType mealType : myMealMap.keySet()) {
        if (MealType.SNACK.equals(mealType)) {
            myMealMap.remove(MealType.SNACK);
        }
    }

    // map changed without throwing Concurrent modification Exception
    System.out.println(myMealMap);

}

  }

Can some one tell me why it is fail safe?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
T-Bag
  • 10,916
  • 3
  • 54
  • 118
  • Read: http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html – Nir Alfasi Feb 26 '15 at 05:30
  • In the map.keySet docs (http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#keySet()) we can read: "If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined" – Mooolo Feb 26 '15 at 05:41
  • 2
    actually foreach loop for set using iterator behind the scene, so you cannot say you are not using iterator for it. – Sanjay Rabari Feb 26 '15 at 05:45

1 Answers1

1

You can see EnumMap.java source and in comments givent that Iterators returned by the collection views are weakly consistent: they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.

One more aspect given at EnumMap Performance Reason

Community
  • 1
  • 1
Karan Verma
  • 69
  • 11