使用Iterator接口迭代HashMap

问题描述 投票:0回答:1

我试图找到数组中每个值出现的唯一次数。我将所有出现的元素放入 HashMap 中。我想检查HashMap中任意两个相邻的值是否相同。 我尝试过使用 Iterator 接口进行迭代,但似乎缺少一些东西

class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        boolean uni = true;
        HashMap<Integer, Integer> hm = new HashMap<>();
        
        for (int a : arr) {
            if (hm.containsKey(a)) {
                hm.put(a, hm.get(a) + 1);
            } else {
                hm.put(a, 1);
            }
        }

        // Iterate over the HashMap entries
        Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
        while (it.hasNext()) {
            if (it.next().getValue() == it.getValue()) {
                uni = false;
                break;
            }
        }
        
        return uni;
    }
}
java arrays hashmap iterator
1个回答
0
投票

请使用 TreeMap 查看以下内容。我意识到 LinkedHashMap 可能不是那么好,因为它遵循插入顺序,并且在您的情况下,您可能希望按键的值对其进行排序。不过,如果您使用 hashmap 或 linkedhashmap,您可能只需获取键并对其进行排序,然后再执行其他操作。

考虑一下迭代器值处理方式的差异。另外,我只比较这些值,只要我能确保有有效的值可以比较。

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2,3, 2,3,1,1,1,3,4};
        boolean bool = uniqueOccurrences(arr);
        System.out.println(bool);
        System.out.println(uniqueOccurrencesMap(arr));
    }
    
    public static boolean uniqueOccurrences(int[] arr) {
        boolean uni = true;
        TreeMap<Integer, Integer> hm = new TreeMap<>();
        
        for (int a : arr) {
            if (hm.containsKey(a)) {
                hm.put(a, hm.get(a) + 1);
            } else {
                hm.put(a, 1);
            }
        }

        // Iterate over the HashMap entries
        Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
        Integer current = null, previous = null;
        while (it.hasNext()) {
            previous = current;  //what was previously current is now previous
            current = it.next().getValue();  //what is next is now current
            if (!(current == null || previous == null)) {  //if any value is null, not comparable
                if (current == previous) {
                    uni = false;
                    break;
                }
            }
        }
        
        return uni;
    }
    
    public static TreeMap<Integer,Integer> uniqueOccurrencesMap(int[] arr) {
        boolean uni = true;
        TreeMap<Integer, Integer> hm = new TreeMap<>();
        
        for (int a : arr) {
            if (hm.containsKey(a)) {
                hm.put(a, hm.get(a) + 1);
            } else {
                hm.put(a, 1);
            }
        }

        // Iterate over the HashMap entries
        Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
        Integer current = null, previous = null;
        while (it.hasNext()) {
            previous = current;  //what was previously current is now previous
            current = it.next().getValue();  //what is next is now current
            if (!(current == null || previous == null)) { //if any value is null, not comparable
                if (current == previous) {
                    uni = false;
                    break;
                }
            }
        }
        
        return hm;
    }

输出:

真实

{1=4, 2=2, 3=3, 4=1}

© www.soinside.com 2019 - 2024. All rights reserved.