如何在迭代时从hashmap中删除键

时间:2014-12-02 15:32:49

标签: java loops hashmap key

我正在迭代一个hashmap,并比较两个键。我将第一个密钥保存到临时密钥后将其删除,因此它永远不会通过keySet进行检查 如果找到与之前的密钥配对,我想删除这两个密钥(或者至少不再迭代该特定密钥)

Iterator<Entry<Double, List<Double>>> i = map.entrySet().iterator();

while(i.hasNext()) {
    Map.Entry<Double, List<Double>> next = i.next();
    i.remove();

    for(Map.Entry<Double, List<Double>> e : map.entrySet()) {

        //if true they are a pair
        if((next.getKey() == Double.POSITIVE_INFINITY && e.getKey() == 0) || (next.getKey() == 0 && e.getKey() == Double.POSITIVE_INFINITY) || Math.abs(next.getKey() * e.getKey() + 1.0) < 0.0000001) {

            List<Double> arrayX = next.getValue();
            List<Double> arrayY = e.getValue();

            for(int a = 0; a < arrayX.size(); a++) {
                for(int b = 0; b < arrayY.size(); b++) {
                    if(Math.abs(arrayX.get(a) - arrayY.get(b)) < 0.0000001) {
                    squares++;
                    }
                }
            }

        }
    }
}

视觉演示的随机示例:

{1,2,3,1,2,4,5,6}
1 against {2,3,1,2,4,5,6}
when it reaches 1 remove it and perform a function
so the list left is {2,3,2,4,5,6}
repeat until all the list has been traversed

我怎样才能做到这一点?

0 个答案:

没有答案