如何遍历嵌套地图和多集? -Java/番石榴 [英] How to iterate through Nested Map and Multiset? - Java/Guava

查看:59
本文介绍了如何遍历嵌套地图和多集? -Java/番石榴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用这样的声明遍历嵌套地图?

How should I iterate through a Nested Map with such declaration?

  • Map<String, Multiset<String>>

请建议是否还有其他哈希映射/列表更有效地完成此哈希填充任务?

Please suggest if there are other hashmap/list that are more effective way of doing this hash population task?

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

因此,现在填充了哈希图,如何遍历该复杂的嵌套图? 我已经尝试了下面的代码,但仅从Multiset中获得了第一个getValue():

So now the hashmap is populated, how do i iterate through that complex nested map? I've tried the code below but I only got the 1st getValue() from my Multiset:

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

推荐答案

在您的代码中,您没有将Multiset添加到Map中.这就是为什么您看不到任何输出的原因.

In your code you aren't adding your Multiset to your Map. That's why you are not seeing any output.

在您的代码中,我这样做:

In your code I did this:

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

在循环内部,然后使用相同的迭代器,我可以看到所有这样的输出:

inside the loop, and then with the same iterator I can see all the outputs like this :

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]

完整的代码以供参考:

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

这篇关于如何遍历嵌套地图和多集? -Java/番石榴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆