2011-12-15 10 views
0

このような宣言でネストされたマップをどのように反復すべきですか?ネストされたマップとマルチセットをどのように反復処理するのですか? - Java/Guava

  • Map<String, Multiset<String>>

このハッシュ人口タスクを行うための、より効果的な方法です他のハッシュマップ/リストがある場合にお勧めしてください?

import com.google.common.collect.Multiset; 
import com.google.common.collect.TreeMultiset; 

String[] foobarness = {"foo" , "bar", "ness", "foo", 
    "bar", "foo", "ness", "bar", "foo", "ness", "foo", 
    "bar", "foo", "ness", "bar", "ness", "foo", "bar", 
    "foo", "ness"}; 
String[] types = {"type::1", "type::2", "type::3", 
    "type::4",}; 

Map<String, Multiset<String>> typeTextCount = 
new HashMap<String, Multiset<String>>(); 

Multiset<String> textAndCount 
    = TreeMultiset.create(); 

for (int i=0; i<types.length; i++) { 
    // I know it's kinda weird but in my task, 
    // i want to keep adding only 1 to the count for each entry. 
    // Please suggest if there is a better hashmap/list for such task. 
    if ((types[i]== "type::1") or (types[i]== "type::3")) { 
     for (String text : foobarness) { 
      // I don't worry too much about how i 
      // populate the Map, it is iterating through 
      // the Map that I have problem with.   
      textAndCount.put(text, 1); 
     } 
    } 

    if ((types[i]== "type::2") or (types[i]== "type::4")) { 
     for (String text : foobarness) 
      textAndCount.put(text, 1); 
    } 
} 

ここでハッシュマップにデータが設定されていますが、どのように複雑なネストマップを反復処理しますか? は、私は以下のコードを試してみたが、私は私の多重集合からの第一のgetValue()を得た:あなたのコードで

Iterator<Entry<String, Multiset<String>>> itTTC = 
    typeTextCount.entrySet().iterator(); 
while (itTTC.hasNext()) { 
    Map.Entry textCt = (Map.Entry)itTTC.next(); 
    System.out.println(textCt.getKey() + " :\t" + textCt.getValue()); 
    itTTC.remove(); 
} 
+0

ここで、要素をマップに配置しますか? – soulcheck

+0

ところで、デバッガを試してみましたか? – Scorpion

答えて

2

はあなたのMultisetあなたMapに追加されていません。だからあなたは何の出力も見ていないのです。あなたのコードで

は、私はこれでした:ループ内

typeTextCount.put(types[i], textAndCount); 

をし、同じイテレータで私はこのようなすべての出力を見ることができます:

type::3 : [bar x 24, foo x 32, ness x 24] 
type::2 : [bar x 24, foo x 32, ness x 24] 
type::4 : [bar x 24, foo x 32, ness x 24] 
type::1 : [bar x 24, foo x 32, ness x 24] 

はEDIT:参照のための完全なコード:

import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.Map.Entry; 

import com.google.common.collect.Multiset; 
import com.google.common.collect.TreeMultiset; 

public class TestIterator { 

    private static String[] foobarness = 
            { 
      "foo", "bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "bar", "foo", "ness", 
      "bar", "ness", "foo", "bar", "foo", "ness" 
            }; 
    private static String[] types  = 
            { 
      "type::1", "type::2", "type::3", "type::4", 
            }; 
    public static void main(String[] args) { 
     Map<String, Multiset<String>> typeTextCount = new HashMap<String, Multiset<String>>(); 

     Multiset<String> textAndCount = TreeMultiset.create(); 

     for (int i = 0; i < types.length; i++) { 
      // I know it's kinda weird but in my task, 
      // I want to keep adding only 1 to the count for each entry. 
      // Please suggest if there is a better hashmap/list for such task. 
      if (("type::1".equals(types[i])) || ("type::3".equals(types[i]))) { 
       for (String text : foobarness) { 
        // I don't worry too much about how i 
        // populate the Map, it is iterating through 
        // the Map that I have problem with. 
        textAndCount.add(text, 1); 
       } 
      } 

      if (("type::2".equals(types[i])) || ("type::4".equals(types[i]))) { 
       for (String text : foobarness) 
        textAndCount.add(text, 1); 
      } 
      typeTextCount.put(types[i], textAndCount); 
     } 

     Iterator<Entry<String, Multiset<String>>> itTTC = typeTextCount.entrySet().iterator(); 
     while (itTTC.hasNext()) { 
      Map.Entry textCt = (Map.Entry) itTTC.next(); 
      System.out.println(textCt.getKey() + " :\t" + textCt.getValue()); 
      itTTC.remove(); 
     } 
    } 
} 
+2

私はちょうど==を文字列の平等のために使用していることを指摘したいと思います。これはほとんど確かにTERRIBLE IDEAです。 –

+0

修正ありがとう:)前回のOPコードではほとんど変更がありませんでしたが、完璧な目はそうです;) – mprabhat

+1

は==に記載されています。 =)、悪い習慣は、regexy perl、文字通りのpythonとtexty prologによってあまりにも甘やかされます。 – alvas

関連する問題