你如何打印bag数据类型?

时间:2009-02-18 04:43:17

标签: java

Bag<String> wordFrequencies = getWordFrequencies(text);

我如何看到这个单词包含的内容...  我用过 org.apache.commons.collections15.Bag org.apache.commons.collections15.bag.HashBag包

1 个答案:

答案 0 :(得分:2)

public static String bagToString(Bag<?> bag) {
    StringBuilder sb = new StringBuilder();
    for (Object o : bag.uniqueSet() {

        sb.append(o);
        sb.append(":");
        int nItems = bag.getCount(o);
        sb.append(nItems);
        sb.append(" ");
    }
    return sb.toString();
}