2012-09-26 41 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你想要做的..什麼