map.getOrDefault(key,replace_words)的用法

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, String> testMap=new HashMap<String, String>();
		testMap.put("段兿", "澳大利亚");
		testMap.put("程思莺", "澳大利亚");
		testMap.put("大佬项", "中国");
		String address1=testMap.getOrDefault("段兿", "中国");
		String address2=testMap.getOrDefault("江小白", "中国");
		System.out.println(address1);
		System.out.println(address2);
	}

map.getOrDefault(key,replace_words)其实就相当于map.get(key)的升级版,如果map中含有对应的key,则相当于get(),如果没有则输出replace_words

输出结果:

猜你喜欢

转载自blog.csdn.net/FaustoPatton/article/details/87876133
今日推荐