【问题标题】:Removing a key-value pair from HashMap is giving an error during for loop从 HashMap 中删除键值对在 for 循环期间会出错
【发布时间】:2012-09-26 08:52:59
【问题描述】:

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

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);
                }
            }
        }  
    }
}

任何人都可以建议即使元素被删除,如何让循环继续进行,还是有更好的方法来做到这一点?

【问题讨论】:

  • 你想达到什么目的?最终结果应该是什么样子?你为什么做这个?您是否尝试对地图进行加权搜索?
  • 这是我试图让它工作的算法的一部分。我将值存储在 HashMap 中,因为我需要一种以某种方式迭代列表的方法。这是一种愚蠢的做法吗?
  • 你是怎么决定你的myMapCount 是6??
  • @Cryssie 好的,你想达到什么目的?最终结果应该是什么样子?您是否尝试对地图进行加权搜索?

标签: java for-loop nullpointerexception hashmap


【解决方案1】:

第一次迭代

indexKey = 0-1;
   compareKey=1-0;

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

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

编辑:

正如 Fildor 在 cmets 中所说:

check if myMap.get(indexKey) and myMap.get(compareKey) are NUll  
IF Null
Continue your innermost loop
else continue what ever you were doing .

【讨论】:

  • ... 为了让事情继续下去,检查 get(indexKey) 和 get(compareKey) 是否为空,如果是则继续。
  • @Fildor 我不确定 OP 想要实现什么。我只是指出 myMap.get(compareKey) 将返回 null。因此不完整的答案:)
  • 好吧,他问“任何人都可以建议即使元素被删除,如何让循环继续进行”......所以,你的回答 + 我的评论 = 解决方案 :)
  • 只是为了澄清..我是一个“她”和 Fildor,这是个好主意。只是为了确保我理解你的答案..我仍然可以从 HashMap 中删除元素,但我需要为空值添加一个检查条件以保持循环继续进行?
  • @Cryssie yepp,你必须在迭代时检查空值。
【解决方案2】:

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

因为您必须将“0-1”与所有以“1-”开头的元素进行比较,如果您发现所有元素都小于“0-1”,那么只有您将它们删除..所以,您将不得不遍历您的地图,以删除它们..

更好的方法是创建另一个map,您可以在发现元素“0-1”更大之后将其放入其中..

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

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 来做你想做的事,你宁愿想找到一个更好的方法..

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多