如何从multihashmap中拆分值?

时间:2013-06-05 20:29:28

标签: java collections apache-commons

如何拆分值?

public static void main(String[] args) { 
    // Map<String,Set<String>> dep = new HashMap<String,Set<String>>();
    MultiHashMap mp = new MultiHashMap();

    mp.put("a", "10");
    mp.put("a", "12"); 
    mp.put("a", "11"); 
    mp.put("b", "1"); 
    mp.put("c", "14"); 
    mp.put("e", "");
    mp.put("b", "1");
    mp.put("b", "2");
    mp.put("b", "3");
    List list = null; 

    Set set = mp.entrySet();              
    Iterator i = set.iterator(); 

    while (i.hasNext()) { 
        Map.Entry me = (Map.Entry) i.next(); 
        list = (List) mp.get(me.getKey());

        int itemCount = list.size();
        for (int z = 0; z < itemCount; z++) {
            System.out.println(me.getKey());
            System.out.println(me.getValue());

        }
    }                  
}

但我得到了值

e
[]
b
[1, 1, 2, 3]
b
[1, 1, 2, 3]
b
[1, 1, 2, 3]
b
[1, 1, 2, 3]
c
[14]
a
[10, 12, 11]
a
[10, 12, 11]
a
[10, 12, 11]

但我需要将值显示为

a:10
a:12
a:11
b:1
b:1
b:2
b:3
c:14
e:

如何打印如上所示的值?

3 个答案:

答案 0 :(得分:0)

打印时显式循环显示值:

for (int z = 0; z < itemCount; z++) {
    for (Object value : (Iterable) me.getValue()) {
        System.out.println(String.format("%s: %s", me.getKey(), value));    
    }
}

还要考虑使用Guava Multimap,它具有适当的泛型支持。

答案 1 :(得分:0)

这令人恐惧,做了你想要的事情:

package com.sandbox;

import org.apache.commons.collections.MultiHashMap;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Sandbox {

    public static void main(String[] args) {
        MultiHashMap mp = new MultiHashMap();
        mp.put("a", "10");
        mp.put("a", "12");
        mp.put("a", "11");
        mp.put("b", "1");
        mp.put("c", "14");
        mp.put("e", "");
        mp.put("b", "1");
        mp.put("b", "2");
        mp.put("b", "3");

        for (Object aSet : getSortedSet(mp)) {
            Map.Entry me = (Map.Entry) aSet;

            for (Object o : (List) me.getValue()) {
                System.out.print(me.getKey());
                System.out.print(": ");
                System.out.println(o);
            }
        }
    }

    private static SortedSet getSortedSet(MultiHashMap mp) {
        Set set = mp.entrySet();

        SortedSet sortedSet = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                return ((String) e1.getKey()).compareTo((String) e2.getKey());
            }
        });
        sortedSet.addAll(set);
        return sortedSet;
    }
}

输出:

a: 10
a: 12
a: 11
b: 1
b: 1
b: 2
b: 3
c: 14
e: 

我建议你提出所有人的建议并使用Guava Multimap。

答案 2 :(得分:0)

试试这个

public static void main(String[] args) {
    // Map<String,Set<String>> dep = new HashMap<String,Set<String>>();
    MultiHashMap mp = new MultiHashMap();
    mp.put("a", "10");
    mp.put("a", "12");
    mp.put("a", "11");
    mp.put("b", "1");
    mp.put("c", "14");
    mp.put("e", "");
    mp.put("b", "1");
    mp.put("b", "2");
    mp.put("b", "3");
    List list = null;

    Set set = mp.entrySet();
    Iterator i = set.iterator();
    while (i.hasNext()) {

        Map.Entry<String, List<String>> me = (Map.Entry) i.next();

        for(int j = 0 ; j< me.getValue().size(); j++ )
        {
            System.out.println(me.getKey() +" : " +me.getValue().get(j));
        }
    }
}

输出

e : 
b : 1
b : 1
b : 2
b : 3
c : 14
a : 10
a : 12
a : 11