如何从multihashmap中分割值?

suzh9iv8  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(484)

如何拆分值?

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:

如何像上面那样打印值?

oiopk7p5

oiopk7p51#

打印时显式循环值:

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 ,它有适当的泛型支持。

wmomyfyw

wmomyfyw2#

这一切,在它所有的恐怖,做你想要的:

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多重Map。

o2rvlv0m

o2rvlv0m3#

试试这个

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

相关问题