首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从HashMap中删除键值对会在for循环期间产生错误

从HashMap中删除键值对会在for循环期间产生错误
EN

Stack Overflow用户
提问于 2012-09-26 16:53:00
回答 2查看 1.7K关注 0票数 0

我在HashMap中存储了一组数据。对数据中的元素进行比较,如果满足条件,则从元素中删除这些元素。但是,我使用for循环来迭代元素,它给了我一个Java Null指针错误。

代码语言:javascript
复制
Example of comparisons:

Item: 0-1
Item: 0-2
Item: 0-3
Item: 0-4
Item: 1-2
Item: 1-3
Item: 1-4
Item: 2-3
Item: 2-4
Item: 3-4

Condition: IF Item 0-1 > (1-2, 1-3 and 1-4): store value 0-1 in another array 
           then remove Item 0-1, 1-2, 1-3 and 1-4 from HahsMap list. ELSE continue to next set
Condition: IF Item 0-2 > (2-3 and 2-4): store value 0-2 in another array 
           then removed Item 0-2, 2-3 and 2-4 from HahsMap list. ELSE continue to next set.

import java.util.HashMap;
import java.util.Map;

public class TestHashMapLoop {
    public static void main(String[] args) 
    {
        Map<String, Integer> myMap = new HashMap<String, Integer>();

        myMap.put("0-1", 33);
        myMap.put("0-2", 29);
        myMap.put("0-3", 14);
        myMap.put("0-4", 8);
        myMap.put("0-5", 18);
        myMap.put("1-2", 41);
        myMap.put("1-3", 15);
        myMap.put("1-4", 17);
        myMap.put("1-5", 28);
        myMap.put("2-3", 1);
        myMap.put("2-4", 16);
        myMap.put("2-5", 81);
        myMap.put("3-4", 12);
        myMap.put("3-5", 11);
        myMap.put("4-5", 21);

        int myMapCount = 6;

        for(int i = 0; i < myMapCount; i++)
        {
            for(int j = i+1; j < myMapCount; j++)
            {
                String indexKey = i+"-"+j;

                for(int k = 0; k < myMapCount; k++)
                {
                    String compareKey = j+"-"+k;                        
                    System.out.println("Index " + indexKey + " : " + compareKey);

                    if((myMap.get(indexKey)) > (myMap.get(compareKey)))
                    {
                        //Store value indexKey in another array (not shown here)
                        System.out.println("Index" + myMap.get(compareKey) + " is removed..");
                        myMap.remove(compareKey);
                    }
                    System.out.println("Index " + myMap.get(indexKey) + " is removed..");
                    myMap.remove(indexKey);
                }
            }
        }  
    }
}

有没有人可以建议如何在元素被移除的情况下让循环继续运行,或者有更好的方法来做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-26 17:00:20

在第一次迭代中

代码语言:javascript
复制
indexKey = 0-1;
   compareKey=1-0;

if((myMap.get(indexKey)) > (myMap.get(compareKey)))

您的mymap.get("1-0")将返回

编辑:

正如Fildor在评论中所说:

代码语言:javascript
复制
check if myMap.get(indexKey) and myMap.get(compareKey) are NUll  
IF Null
Continue your innermost loop
else continue what ever you were doing .
票数 0
EN

Stack Overflow用户

发布于 2012-09-26 17:47:44

删除valueless而不是特定key-value对的所有key-value将是一项微不足道的任务。

因为您必须将"0-1“与所有以"1-”开头的元素进行比较,如果您发现所有元素都小于"0-1",则只有您才会删除它们。因此,您将不得不遍历您的map,以删除它们。

更好的方法是创建另一个map,在发现元素"0-1“较大后,可以将它放入其中。

我宁愿使用增强的for循环..

代码语言:javascript
复制
public class TestHashMapLoop {
    public static void main(String[] args) 
    {
        Map<String, Integer> myMap = new HashMap<String, Integer>();
        Map<String, Integer> newMap = new HashMap<String, Integer>();

        /** Initialize Map **/

        boolean flag = true;
        Set<String> keySet = myMap.keySet();

        for (String key: keySet) {
            flag = true;
            for (String innerKey: keySet) {

                if (innerKey.startsWith(String.valueOf(key.charAt(2)))) {

                    if (myMap.get(key) > myMap.get(innerKey)) {
                        continue;

                    } else {
                        flag = false;
                        break;
                    }
                }

            }
            if (flag) {
                newMap.put(key, myMap.get(key));
            }
        }
        System.out.println(newMap);
    }
}

但是,这也不是一个很好的方式。注意,通过这种方式,您将在map with n keys :- n * n times上迭代

你宁愿找到一种更好的方法,而不是使用HashMap来做你想做的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12597967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档