zoukankan      html  css  js  c++  java
  • Java中map.getOrDefault()方法的使用

    Map.getOrDefault(Object key, V defaultValue)方法的作用是:
      当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
    
  • 相关阅读:
    bzoj1098 1301
    bzoj3237
    bzoj3170
    bzoj4008
    一些题解
    bzoj4028
    bzoj3196
    redis学习
    quartz学习
    电商618 压测、优化、降级预案
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076406.html
Copyright © 2011-2022 走看看