简体   繁体   中英

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

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]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM