Java 从HashMap中删除键值对会在for循环期间出错

Java 从HashMap中删除键值对会在for循环期间出错,java,for-loop,nullpointerexception,hashmap,Java,For Loop,Nullpointerexception,Hashmap,我在HashMap中存储了一组数据。比较数据中的元素,如果满足条件,则从元素中删除这些元素。然而,我使用for循环来迭代元素,它给了我一个Java空指针错误 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

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

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);
                }
            }
        }  
    }
}
比较示例:
项目:0-1
项目:0-2
项目:0-3
项目:0-4
项目:1-2
项目:1-3
项目:1-4
项目:2-3
项目:2-4
项目:3-4
条件:如果项0-1>(1-2、1-3和1-4):将值0-1存储在另一个数组中
然后从HahsMap列表中删除项目0-1、1-2、1-3和1-4。否则继续下一盘
条件:如果项0-2>(2-3和2-4):将值0-2存储在另一个数组中
然后从HahsMap列表中删除项目0-2、2-3和2-4。否则继续下一盘。
导入java.util.HashMap;
导入java.util.Map;
公共类TestHashMapLoop{
公共静态void main(字符串[]args)
{
Map myMap=newhashmap();
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;
对于(int i=0;i(myMap.get(compareKey)))
{
//将值indexKey存储在另一个数组中(此处未显示)
System.out.println(“Index”+myMap.get(compareKey)+“已删除…”);
myMap.remove(compareKey);
}
System.out.println(“Index”+myMap.get(indexKey)+“已删除”);
myMap.remove(索引键);
}
}
}  
}
}

有人能建议如何在删除元素的情况下继续循环吗?或者有更好的方法吗?

在第一次迭代中

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

if((myMap.get(indexKey)) > (myMap.get(compareKey)))
您的mymap.get(“1-0”)将返回null

编辑:

正如Fildor在评论中所说:

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-1”与所有以“1-”开头的元素进行比较,如果您发现所有元素都小于“0-1”,则只有您将删除它们。。因此,您必须在地图上迭代,才能删除它们

更好的方法是创建另一个
映射
,您可以在发现元素“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);
    }
}
公共类TestHashMapLoop{
公共静态void main(字符串[]args)
{
Map myMap=newhashmap();
Map newMap=newhashmap();
/**初始化映射**/
布尔标志=真;
Set keySet=myMap.keySet();
用于(字符串键:键集){
flag=true;
用于(字符串内键:键集){
if(innerKey.startsWith(String.valueOf(key.charAt(2))){
if(myMap.get(key)>myMap.get(innerKey)){
继续;
}否则{
flag=false;
打破
}
}
}
国际单项体育联合会(旗){
newMap.put(key,myMap.get(key));
}
}
System.out.println(newMap);
}
}
但是,这也不是一个很好的方法。请注意,这样您就可以使用n个键在
映射上进行迭代:-n*n次


对于您想做的事情,您宁愿想找到一种比使用HashMap更好的方法。

您想实现什么?最终结果应该是什么样的?你为什么这么做?你是在尝试对地图进行加权搜索吗?这是我试图让它工作的算法的一部分。我将这些值存储在HashMap中,因为我需要一种以某种方式迭代列表的方法。这是一种愚蠢的做法吗?你是如何决定你的
myMapCount
将是6的???@Cryssie好的,你想实现什么?最终结果应该是什么样的?你正在尝试对地图进行加权搜索吗?。。。为了让事情继续进行,请检查get(indexKey)和get(compareKey)是否为null,如果为null则继续。@Fildor我不确定OP想要实现什么。我只是指出myMap.get(compareKey)将返回null。因此不完整的答案:)他问“即使元素被移除,谁能建议如何让循环继续”。。。所以,你的回答+我的评论=解决方案:)只是想澄清一下……我是一个“她”和费尔多,这是个好主意。为了确保我理解你的答案……我仍然可以从HashMap中删除元素,但我需要添加一个空值检查条件以保持循环继续?@Cryssie yepp,你必须在迭代时检查空值。