简体   繁体   中英

How to join two maps in java?

I have the following Map:

LonM =[0, 0, 0, 0, 46, 15]
LatM =[5, 52, 35, 16, 37, 5]

Paralelly, I have implemented a composite pattern class:

public class CompositeDataFrame implements IDataFrame, ICompositeDataFrame {

    private String name;
    private List <IDataFrame> children;

    public CompositeDataFrame(String name){
        this.name = name;
        children = new LinkedList<>();
    }

    public void addChildren(IDataFrame child){
        children.add(child);
    }

And I've obtained the following query method:

public Map<Object, List<Object>> query(String keySelector, Predicate<Object> valuePredicate) {
    Map<Object, List<Object>> result = new HashMap<>();
    for (IDataFrame child:children)
        result.putAll(child.query(keySelector, valuePredicate));
    return result;
}

So then we have the following main which call the query method:

CompositeDataFrame comp = new CompositeDataFrame("CompositeCities");
        CompositeDataFrame comp1 = new CompositeDataFrame("2");

        comp.addChildren(comp1);
        comp1.addChildren(df);
        comp.addChildren(df);
        
        System.out.println("\n"+comp.query("LonM", entry -> ((Integer) entry) == 0));

The question is: how can I concatenate the map's of each child?

PD: I used the putAll method but it remove the maps which same keys, so it doesn't be useful in this case.

The output should be:

LonM =[0, 0, 0, 0, 0, 0, 0, 0] LatM =[5, 52, 35, 16, 5, 52, 35, 16]

Thanks a lot!

Yes if you use putAll it removes the map entries with same keys. That's how a map works. What you are looking for is a way to group all the entries by key which is somewhat easy to do with Stream s.

Map<Object, List<Object>> result = children.stream()
        // Map from Stream<IDataFrame> to Stream<Map.Entry<Object, List<Object>>>
        .flatMap(child -> child.query(keySelector, predicate).entrySet().stream())
        // Collect back into a Map<Object, List<Object>> using groupinBy
        .collect(Collectors.groupingBy(
            // Use the key to group the values
            Map.Entry::getKey, 
            // Since we don't want a Map<Object, List<List<Object>>> the List needs to be flattend using Collectors.flatMapping.
            Collectors.flatMapping(entry -> entry.getValue().stream(), Collectors.toList())));

Of course you don't have to use Stream s.

Map<Object, List<Object>> result = new HashMap<>();
for (IDataFrame child : children) {
    // Loop over each entry of the query result.
    child.query(keySelector, predicate).forEach((key, value) -> {
        // computeIfAbsent creates a new List if none exists for the given key, otherwise it returns the existing one.
        List<Object> valueList = result.computeIfAbsent(key, o -> new ArrayList<>());
        // Finally just add all the values to the list.
        valueList.addAll(value);
    });
}

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