2011-12-15 73 views
0

通過引用放置HashMap並通過複製放置hashmaps。我如何做後者? 另一個問題是String[] types的數量不是真正的預知,因此創建Multiset<String> textAndCount = TreeMultiset.create();的多個實例並不是很有幫助。 我有下面的代碼,但兩種類型的輸出都是一樣的。HashMap put或putAll? - Java

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[] foobarness2 = {"bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "bar", "foo", "ness", "ness", "bar", "foo", "ness"}; 

private static String[] types = {"type::1", "type::2"}; 


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++) { 
     if ("type::1".equals(types[i])) { 
      for (String text : foobarness) 
       textAndCount.add(text, 1); 
     } 
     if ("type::2".equals(types[i])) { 
      for (String text : foobarness2) 
       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(); 
    } 
} 

我的輸出是從上面的代碼:

type::2 : [bar x 13, foo x 17, ness x 14] 
type::1 : [bar x 13, foo x 17, ness x 14] 

正確的輸出應該是:

type::1 : [bar x 6, foo x 8, ness x 6] 
type::2 : [bar x 7, foo x 9, ness x 8] 

回答

4

移動Multiset<String> textAndCount = TreeMultiset.create()您的for循環中。這兩個「類型」共享同一個multiset,因此您的計數加倍。

您的循環,然後可能是這樣的:

for (int i = 0; i < types.length; i++) { 
     Multiset<String> textAndCount = TreeMultiset.create(); 
     if ("type::1".equals(types[i])) { 
      for (String text : foobarness) 
       textAndCount.add(text, 1); 
     } 
     if ("type::2".equals(types[i])) { 
      for (String text : foobarness2) 
       textAndCount.add(text, 1); 
     } 
     typeTextCount.put(types[i], textAndCount); 
    } 

當你在這,你可以通過使用換每種款式循環改善你的地圖上的迭代爲好。如果您在迭代時想要刪除每個條目,則可以將您的entrySet包裝在consumingIterable中以獲得相同的功能。

for (Entry<String, Multiset<String>> textCt : Iterables.consumingIterable(typeTextCount 
      .entrySet())) { 
     System.out.println(textCt.getKey() + " :\t" + textCt.getValue()); 
    } 

這樣產生的輸出:

type::2 : [bar x 7, foo x 9, ness x 8] 
type::1 : [bar x 6, foo x 8, ness x 6] 

如果你不喜歡這樣的順序,我建議使用Ordering,讓您的條目排序列表。

0

Multiset<String> textAndCount = TreeMultiset.create();應該在循環內部。如果你把一組的副本輸出將是

 
type::1 : [bar x 6, foo x 8, ness x 6] 
type::2 : [bar x 13, foo x 17, ness x 14]