Конвертировать карту на карту после группировки - PullRequest
0 голосов
/ 06 февраля 2020

Нужна помощь

Как конвертировать

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

после groupingBy

Заранее спасибо

1 Ответ

0 голосов
/ 06 февраля 2020
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]}

...