0

参照によるハッシュマップの配置とコピーによるハッシュマップの配置。後者はどうすればいいですか?もう 1 つの問題は、 の数がString[] types実際には事前にわかっていないため、 の複数のインスタンスを作成してMultiset<String> textAndCount = TreeMultiset.create();もあまり役に立ちません。次のコードがありますが、両方のタイプの出力は同じです。

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

2 に答える 2

4

Multiset<String> textAndCount = TreeMultiset.create()for ループ内に移動します。この同じマルチセットが両方の「タイプ」で共有されているため、カウントが 2 倍になります。

for ループは次のようになります。

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

その間に、for-each スタイル ループを使用して、マップの繰り返しを改善することもできます。consumingIterable繰り返し処理するときに各エントリを削除したい場合は、同じ機能のために entrySet を でラップできます。

    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てエントリのソート済みリストを取得することをお勧めします。

于 2011-12-15T18:49:06.130 に答える
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]
于 2011-12-15T18:49:39.507 に答える