leetcode350. 两个数组的交集 II
官方题解中使用了哈希表的方法,其中有map.getOrDefault这个方法,就是当Map集合中有这个key时,就使用这个key对应的value值,如果没有这个key就使用默认值defaultValue
Map<Integer, Integer> map = new HashMap<>();
map.put(1,5);
map.put(2,5);
map.put(3,5);
int count = map.getOrDefault(1, 2);
System.out.println(count);// 5,map中存在1,获得1对应的value
int count1 = map.getOrDefault(4, 0);
System.out.println(count1);//0,map中不存在4,使用默认值“0”