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

206
Views
Check whether a map contains all contents of another map

I am trying to check whether a map contains all contents of another map. For example, I have a mapA which is a Map<String, List<String>> and the elements are:

"1" -> ["a","b"]
"2" -> ["c","d"]

another mapB which is also a Map<String, List<String>>, the elements are:

"1" -> ["a"]
"2" -> ["c","d"],

I want to create a function compare(mapA, mapB) which will return false in this case.

What is the best way to do this?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Inside your compare(mapA, mapB) method, you can simply use:

return mapA.entrySet().containsAll(mapB.entrySet());
about 4 years ago · Santiago Trujillo Report

0

Try this code :

Assert.assertTrue(currentMap.entrySet().containsAll(expectedMap.entrySet()));

about 4 years ago · Santiago Trujillo Report

0

you can try this.

static boolean compare(Map<String, List<String>> mapA, Map<String, List<String>> mapB){
        return mapA.entrySet().containsAll(mapB.entrySet());
    }

As suppose, provided data is something like this:

            Map<String, List<String>> mapA = new HashMap<>();
            Map<String, List<String>> mapB = new HashMap<>();

            mapA.put("1", Arrays.asList("a","b"));
            mapA.put("2", Arrays.asList("c","d"));

            mapB.put("1", Arrays.asList("a"));
            mapB.put("2", Arrays.asList("c", "d"));

            System.out.println(compare(mapA, mapB));

In this case compare(mapA, mapB) method will return false. But suppose provided data is something like this:

            Map<String, List<String>> mapA = new HashMap<>();
            Map<String, List<String>> mapB = new HashMap<>();

            mapA.put("1", Arrays.asList("a","b"));
            mapA.put("2", Arrays.asList("c","d"));

            mapB.put("1", Arrays.asList("a", "b"));
            mapB.put("2", Arrays.asList("c", "d"));

            System.out.println(compare(mapA, mapB));

In this case, compare(mapA, mapB) method, which I have written will return true.

compare(mapA, mapB) method basically checking for all the entries in mapA with mapB, if same returning yes, else returning false;

about 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!