2016-04-05 13 views
0

hashMapを1つの文字列の値で分割するにはどうすればよいですか?たとえば、key:attribute1の値: "a"で下のhashMapテーブルを分割します。ハッシュマップをその文字列の値に基づいて分割する

public Map<String, List> table = new HashMap<String, List>(); 

     String[] attribute1 = {"a", "a", "b", "c", "c"}; 
     String[] attribute2 = {"car", "car", "car", "train", "bus"}; 
     String[] attribute3 = {"yes", "no", "no", "no", "yes"}; 

それが与えるように:

 String[] attribute1 = {"a", "a"}; 
     String[] attribute2 = {"car", "car"}; 
     String[] attribute3 = {"yes", "no", }; 

を私が対応する値のインデックスを持つキーのすべての値を削除することによってこれを行うことができます方法はありますか?私が学んだことから、あなたは値が地図を持っていないのでできません。

+1

質問があいまいです。 this.popItemはこの問題と何をしていますか? –

+0

申し訳ありませんが、私のコードでどのように使用されているのかが含まれています – OasisAsh

+0

なぜ 'this'という接頭辞を使用しますか?それはJVMによって自動的に含まれています、 – Majora320

答えて

0

これは、テーブル内のすべてのエントリが正しい列と、ループトラフを見つけるだけの問題だと正しい列を抽出します。

public static Map<String,List> filterMap(Map<String,List> table, String key, String filter) { 
    Map<String,List> m = new HashMap<String,List>(); 
    List l = table.get(key); 
    if (l != null) { 
     List<Integer> cols = new List<Integer>(); 
     for (int i = 0; i < l.size(); i++) { 
     if ((filter == null && l.get(i) == null) || (filter != null && filter.equals((String)l.get(i)))) { 
      cols.add(new Integer(i)); 
     } 
     } 
     // for each entry in table 
     // extract the items saved in cols and add it to m 
    } 
    return m; 
}