Java中map.getOrDefault()方法的使用

喜夏-厌秋 提交于 2020-07-27 07:07:25

HashMap<String, String> map = new HashMap<>();

map.put("name", "cookie");

map.put("age", "18");

map.put("sex", "女");

String name = map.getOrDefault("name", "random");

System.out.println(name);// cookie,map中存在name,获得name对应的value

int score = map.getOrDefault("score", 80);

System.out.println(score);// 80,map中不存在score,使用默认值80

在这里看到的: https://blog.csdn.net/m0_37827190/article/details/106223703?utm_medium=distribute.pc_category.none-task-blog-hot-11.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-11.nonecase&request_id=###

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!