简体   繁体   English

转换地图<String, String>到地图<String, List<String> &gt; 分组后

[英]Convert Map<String, String> to Map<String, List<String>> after GroupingBy

Need some help需要一些帮助

How to convert如何转换

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

after groupingBy groupingBy

Thanks in advance提前致谢

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

And the output和输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 8 流:如何转换地图<String, List<Integer> &gt; 到地图<Integer, List<String> &gt; 使用 groupingBy(.) - Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.) 地图的 Collectors.groupingBy <long, list<string> &gt; 一些字符串操作</long,> - Collectors.groupingBy for Map<Long, List<String>> with some string manipulation 将字符串转换为 Map 列表 - Convert String to List of Map Java转换列表 <String> 到地图 <String, String> - Java Convert List<String> to Map<String, String> 转换列表<String>到地图<String, String>在 Java 中 - Convert List<String> to Map<String, String> in Java 将字符串转换为列表<map<string, string> &gt;&gt; 用于测试</map<string,> - Convert String to List<Map<String, String>>> for tests 将 map 字符串转换为 map 字符串列表字符串 - Java 7 - convert map string string to map string list string - Java 7 Java8 Stream List <Map <String,Object >> groupingBy和count value - Java8 Stream List<Map<String,Object>> groupingBy and counting value 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 如何转换列表<map<string, object> > 列出<map<string, string> > </map<string,></map<string,> - How to Convert List<Map<String, Object>> to List<Map<String, String>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM