Java 转换地图<;字符串,字符串>;映射<;字符串,列表<;字符串>&燃气轮机;分组后

Java 转换地图<;字符串,字符串>;映射<;字符串,列表<;字符串>&燃气轮机;分组后,java,list,hashmap,java-stream,Java,List,Hashmap,Java Stream,需要帮忙吗 如何转换 Map(String, String) to Map(String, List(String)) 在groupingBy 提前谢谢 public class RKeyDataRule { private int id; private int businessProcessId; private int xpathKeyDataId; public RKeyDataRule(int i, int j, int k) {

需要帮忙吗

如何转换

Map(String, String) to Map(String, List(String))
groupingBy

提前谢谢

public class RKeyDataRule {

    private int id;
    private int businessProcessId;
    private int xpathKeyDataId;

    public RKeyDataRule(int i, int j, int k) {
        this.id = i;
        this.businessProcessId = j;
        this.xpathKeyDataId = k;
    }
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the businessProcessId
     */
    public int getBusinessProcessId() {
        return businessProcessId;
    }
    /**
     * @param businessProcessId the businessProcessId to set
     */
    public void setBusinessProcessId(int businessProcessId) {
        this.businessProcessId = businessProcessId;
    }
    /**
     * @return the xpathKeyDataId
     */
    public int getXpathKeyDataId() {
        return xpathKeyDataId;
    }
    /**
     * @param xpathKeyDataId the xpathKeyDataId to set
     */
    public void setXpathKeyDataId(int xpathKeyDataId) {
        this.xpathKeyDataId = xpathKeyDataId;
    }
}
#
import java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.stream.collector;
导入java.util.stream.StreamSupport;
公开课考试{
公共静态void main(字符串[]args){
Iterable iTrKeyDataRule=Arrays.asList(
新RKeyDataRule(1,1,1),
新RKeyDataRule(2,1,2),
新RKeyDataRule(3,1,3),
新RKeyDataRule(4,1,4),
新RKeyDataRule(5,1,5),
新RKeyDataRule(6,2,6),
新RKeyDataRule(7,2,7),
新RKeyDataRule(8,2,8),
新RKeyDataRule(9,2,9),
新RKeyDataRule(10,2,10)
);
List lRKeyDataRule=new ArrayList();
StreamSupport.stream(itrreydatarule.spliterator(),false)
.filter(规则->规则.getXpathKeyDataId()%2==0)
.forEach(lRKeyDataRule::add);
Map rKeyDataRuleGroupedByProc=lRKeyDataRule.stream().collect(Collectors.groupingBy(RKeyDataRule::getBusinessProcessId));
Map procAndMainXpaths=newhashmap();
for(条目:rKeyDataRuleGroupedByProc.entrySet()){
List lmainXpaths=newarraylist();
entry.getValue().stream().map(m->m.getXpathKeyDataId()).forEach(lmainXpaths::add);
procAndMainXpaths.put(entry.getKey(),lmainXpaths);
}
System.out.println(procAndMainXpaths);
}
}
# 以及输出


{1=[2,4],2=[6,8,10]}

到目前为止您尝试了什么?item..entrySet().stream().collect(Collectors.groupingBy(map->map.getKey());但是我没有得到我想要的{(a,1),(a,2),(a,3),(b,1),(b,2)}我需要{(a,{1,2,3}),(b,{1,2})}如果你能提供真正的java代码,那会很有帮助,这样我们可以看到你尝试了什么,我们可以看到你的输入是什么。您上面的评论似乎表明输入是一个带有重复键(a和b)的映射,这是不可能的。感谢您建议我尝试,我可以解决它
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class Test {

    public static void main(String[] args) {

        Iterable<RKeyDataRule> itrRKeyDataRule = Arrays.asList(
                new RKeyDataRule(1,1,1),
                new RKeyDataRule(2,1,2),
                new RKeyDataRule(3,1,3),
                new RKeyDataRule(4,1,4),
                new RKeyDataRule(5,1,5),
                new RKeyDataRule(6,2,6),
                new RKeyDataRule(7,2,7),
                new RKeyDataRule(8,2,8),
                new RKeyDataRule(9,2,9),
                new RKeyDataRule(10,2,10)
                );
        List<RKeyDataRule> lRKeyDataRule = new ArrayList<RKeyDataRule>();
        StreamSupport.stream(itrRKeyDataRule.spliterator(), false)
            .filter(rule -> rule.getXpathKeyDataId()%2==0)
            .forEach(lRKeyDataRule::add);

        Map<Integer, List<RKeyDataRule>> rKeyDataRuleGroupedByProc = lRKeyDataRule.stream().collect(Collectors.groupingBy(RKeyDataRule::getBusinessProcessId));

        Map<Integer, List<Integer>> procAndMainXpaths = new HashMap<>();
        for(Entry<Integer, List<RKeyDataRule>> entry : rKeyDataRuleGroupedByProc.entrySet()) {
            List<Integer> lmainXpaths = new ArrayList<Integer>();
            entry.getValue().stream().map(m -> m.getXpathKeyDataId()).forEach(lmainXpaths::add);
            procAndMainXpaths.put(entry.getKey(), lmainXpaths);
        }

        System.out.println(procAndMainXpaths);

    }

}