【问题标题】:Convert Map<String, String> to Map<String, List<String>> after GroupingBy在 GroupingBy 之后将 Map<String, String> 转换为 Map<String, List<String>>
【发布时间】:2020-02-06 02:23:18
【问题描述】:

需要帮助

如何转换

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

groupingBy之后

提前致谢

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 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)的映射,这是不可能的。
  • 谢谢你建议我试试,我可以解决它

标签: java list hashmap java-stream


【解决方案1】:
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);

    }

}
#

还有输出

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2015-11-21
    • 2021-12-21
    • 2019-09-10
    • 2018-09-06
    相关资源
    最近更新 更多