Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 752355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:46:18+00:00 2026-05-14T14:46:18+00:00

How to compare two maps by their values? I have two maps containing equal

  • 0

How to compare two maps by their values? I have two maps containing equal values and want to compare them by their values. Here is an example:

    Map a = new HashMap();
    a.put("foo", "bar"+"bar");
    a.put("zoo", "bar"+"bar");

    Map b = new HashMap();
    b.put(new String("foo"), "bar"+"bar");
    b.put(new String("zoo"), "bar"+"bar");

    System.out.println("equals: " + a.equals(b));            // obviously false

How should I change the code to obtain a true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:46:19+00:00Added an answer on May 14, 2026 at 2:46 pm

    Your attempts to construct different strings using concatenation will fail as it’s being performed at compile-time. Both of those maps have a single pair; each pair will have “foo” and “barbar” as the key/value, both using the same string reference.

    Assuming you really want to compare the sets of values without any reference to keys, it’s just a case of:

    Set<String> values1 = new HashSet<>(map1.values());
    Set<String> values2 = new HashSet<>(map2.values());
    boolean equal = values1.equals(values2);
    

    It’s possible that comparing map1.values() with map2.values() would work – but it’s also possible that the order in which they’re returned would be used in the equality comparison, which isn’t what you want.

    Note that using a set has its own problems – because the above code would deem a map of {“a”:”0″, “b”:”0″} and {“c”:”0″} to be equal… the value sets are equal, after all.

    If you could provide a stricter definition of what you want, it’ll be easier to make sure we give you the right answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hav two stl maps like map<int,int> and i want to compare them.. so
In java, I want to compare two maps, like below, do we have existing
I need to compare two dictionary values if the types stored are equal, this
I want to compare two xml raw files and capture their differences in data.
I want to compare two text files that i have generated from one of
I want to compare two tabdelimeted files. I take the files and converts them
This is kind of last resort.. So I have two maps. typedef std::map <string,
I want to compare two Java Map s by a simple hash. Each object
I am trying to compare two decimal values in Java script. I have two
How can I compare two parts of number and matched only equal of them?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.