0

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

要素が削除された場合でもループを実行する方法について誰かアドバイスできますか、またはこれを行うためのより良い方法はありますか?

4

2 に答える 2

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 .
于 2012-09-26T09:00:20.477 に答える
0

特定のペア以外のをすべて削除するのkeysvalue簡単な作業です。lesskey-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

やりたいことに a を使用するよりも、より良い方法を見つけたいと思うでしょうHashMap..

于 2012-09-26T09:47:44.493 に答える