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 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:

如何像上面那样打印值?

4

3 回答 3

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。

于 2013-06-05T20:45:36.670 回答
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

于 2013-06-05T20:35:44.673 回答
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
于 2013-06-05T20:48:53.410 回答