Java.util.EnumMap 示例详解编程语言
2023-06-13 09:20:30 时间
//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);
Welcome to meal planner, we have suggestions for following meals : [BREAKFAST, LUNCH, SNACK, DINNER] Q: What should I have for lunch? A: Enjoy Chicken, Rice and bread for Lunch! Q: What should I have for snack? A: How about an apple for the evening snack! Q: What should I have for dinner? A: Keep the dinner light, lets have some salad! Enjoy Milk and Eggs for breakfast! Enjoy Chicken, Rice and bread for Lunch! How about an apple for the evening snack! Keep the dinner light, lets have some salad! *** Checking for concurrent modification exception! *** {BREAKFAST=Enjoy Milk and Eggs for breakfast!, LUNCH=Enjoy Chicken, Rice and bread for Lunch!, DINNER=Keep the dinner light, lets have some salad!}
Welcome to meal planner, we have suggestions for following meals : [BREAKFAST, LUNCH, SNACK, DINNER] Q: What should I have for lunch? A: Enjoy Chicken, Rice and bread for Lunch! Q: What should I have for snack? A: How about an apple for the evening snack! Q: What should I have for dinner? A: Keep the dinner light, lets have some salad! Enjoy Milk and Eggs for breakfast! Enjoy Chicken, Rice and bread for Lunch! How about an apple for the evening snack! Keep the dinner light, lets have some salad! *** Checking for concurrent modification exception! *** {BREAKFAST=Enjoy Milk and Eggs for breakfast!, LUNCH=Enjoy Chicken, Rice and bread for Lunch!, DINNER=Keep the dinner light, lets have some salad!}
11086.html
cjava相关文章
- java grouping,介绍 Java 8 groupingBy Collector
- java指定长度数组长度_Java声明数组时不能指定其长度[通俗易懂]
- 浅析Java多态_JAVA多态
- JAVA程序员简历模板_Java工程师简历模板
- 深入理解Java之线程池详解编程语言
- Java 中常用缓存Cache机制的实现详解编程语言
- 二维码的生成和解码Java实现详解编程语言
- java 时间工具类详解编程语言
- java调整图片的亮度详解编程语言
- java 程序计时详解编程语言
- Java加密算法 AES详解编程语言
- 一个简单的绘制饼图的 Java Bean 实例详解编程语言
- 不依赖于任何Java类库的中文转拼音实现详解编程语言
- Java多线程与并发模型之锁详解编程语言
- java监听器详解编程语言
- Java开发中的23种设计模式详解编程语言
- Java学习笔记之八JavaBean中布尔类型使用注意事项详解编程语言
- Java – 如何通过反射调用对象的方法详解编程语言
- java遍历Set集合详解编程语言
- Java中的枚举用法详解编程语言
- Java编程地位不可动摇的12大原因详解编程语言
- Java中枚举的写法和用法详解编程语言
- Java基础之Object类详解编程语言
- Java的各种加密算法详解编程语言
- java集合遍历的几种方式总结及比较详解编程语言
- Java多线程(一):多线程基础详解编程语言
- java代码中在双引号里面添加双引号详解编程语言
- Java中/r和/n的区别详解编程语言
- java语言基础详解编程语言
- Java HttpClient PostMethod详解编程语言
- Java Socket详解编程语言
- Java更新提升Oracle软件性能(java更新oracle)
- java线性表排序示例分享