HashMap

HashMap的getOrDefault()方法
当Map集合中有这个key时,就使用这个key值,如果没有就使用默认值defaultValue

HashMap 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

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.
Example 1:

Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1.
No two values have the same number of occurrences.
class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        Map countMap=new HashMap();
            for(int i=0;i 1,1
                        //put(2,0+1);=> 2,1
                        //put(2,1+1);=> 2,2
                        //put(1,1+1);=>1,2
                        //put(1,2+1);=>1,3
                        //put(3,0+1);=>3,1
                        //1,3   2,2   3,1               
            }
            int mapSize = countMap.size();
                //一共有几对key-value
            Set mapSet = new HashSet(countMap.values());
                //一共有几对不重复的value
            if(mapSet.size()

你可能感兴趣的:(HashMap)