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

时间:2012-09-26 08:53:00

标签: java for-loop nullpointerexception hashmap

我有一组存储在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);
                }
            }
        }  
    }
}

任何人都可以建议如何在循环中移除元素,或者有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

在第一次迭代

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 .

答案 1 :(得分:0)

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

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

更好的方法是创建另一个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); } } ,在您发现它更大之后可以将元素设置为“0-1”。

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

map with n keys :- n * n times

但是,这也不是一个很好的方法。请注意,通过这种方式迭代HashMap

您希望找到一种比使用{{1}}更好的方式来做您想做的事情。