HashMap put或putAll? - Java

时间:2011-12-15 18:32:38

标签: java map guava deep-copy multiset

通过引用放置Hashmaps并按副本放置哈希映射。我怎么做后者? 另一个问题是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]

2 个答案:

答案 0 :(得分:4)

在for-loop中移动Multiset<String> textAndCount = TreeMultiset.create()。这两个“类型”共享同一个多重集,因此您的计数加倍。

您的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样式循环来改善地图的迭代。如果你在迭代它时热衷于删除每个条目,你可以将你的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获取您的参赛作品的排序列表。

答案 1 :(得分: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]