Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 Streams.

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 Streams.

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);
    });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!