HashMap的getOrDefault()方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43274298/article/details/102708511

当Map集合中有这个key时,就使用这个key值,如果没有就使用默认值defaultValue

	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

猜你喜欢

转载自blog.csdn.net/qq_43274298/article/details/102708511
今日推荐