2012-09-26 40 views
0

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

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

什么是你想达到更好的办法?最终结果应该是什么样子?你为什么做这个?你是否想要对地图进行加权搜索? –

+0

这是我试图让它工作的算法的一部分。我将值存储在HashMap中,因为我需要一种以某种方式迭代列表的方法。这是一种愚蠢的做法吗? – Cryssie

+0

你是怎么决定你的'myMapCount'会是6? –

回答

0

在第一次迭代

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

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

您mymap.get( “1-0”)将返回

编辑:

由于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

...并保持事情进行,检查get(indexKey)并获取(compareKey)为null,如果它们继续。 – Fildor

+0

@Fildor我不确定OP想要达到什么效果。我只是指出myMap.get(compareKey)会返回null。因此不完整的答案:) – PermGenError

+0

好吧,他问“任何人都可以建议如何去循环即使元素被删除”...所以,你的答案+我的意见=解决方案:) – Fildor

0

删除所有keysvalueless而非特定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你想要做的..什么