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 7181483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:34:02+00:00 2026-05-28T17:34:02+00:00

I have a List containing HashMaps . Each HashMap in the list might have

  • 0

I have a List containing HashMaps. Each HashMap in the list might have multiple key/value pairs. I want to indexOf on the list to find out the index of the element where the passed in HashMap is. However, the problem is that equals method of HashMap looks at all the entire entrySet while comparing. Which is not what I want.

Example:

    List<HashMap> benefit = new ArrayList<HashMap>();
    HashMap map1 = new HashMap();
    map1.put("number", "1");
    benefit.add(map1);
    HashMap map2 = new HashMap();
    map2.put("number", "2");
    map2.put("somethingelse", "blahblah"); //1
    benefit.add(map2);

    HashMap find = new HashMap();
    find.put("number", "2");
    int index = benefit.indexOf(find);
    if (index >= 0)
        System.out.println(benefit.get(index).get("number"));

The above code does not print anything because of line with //1.

  • What do I have to do so that the above code actually prints 2?
  • Is there a way to implement comparable on the list so that I can define
    my own?
  • 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-28T17:34:03+00:00Added an answer on May 28, 2026 at 5:34 pm

    I think you’re looking for retainAll(), so you can compare only the elements you’re interested in:

    int index = myIndexOf(benefit, find);
    
    ...
    
    static int myIndexOf(List<HashMap> benefit, Map find) {
        int i = 0;
        for (Map map : benefit) {
            Map tmp = new HashMap(map);
            tmp.keySet().retainAll(find.keySet());
            if (tmp.equals(find)) {
                return i;
            }
            i++;
        }
        return -1;
    }
    

    It’s possible, of course, to declare your own subclass of List that overrides the indexOf method with this behaviour. However, I don’t think that’s a good idea. It would violate the contract of the indexOf method:

    returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i)))

    This would be confusing to someone else maintaining the code. You might then think that you could subclass HashMap to redefine equals, but that would violate the symmetry property of Object.equals().

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

Sidebar

Related Questions

I have a list of objects, each containing an Id, Code and Description. I
I have a java properties file containing a key/value pair of country names and
I have a list. It contains x lists, each with y elements. I want
If I have a list containing [alice, bob, abigail, charlie] and I want to
I have a list containing 98 items. But each item contains 0, 1, 2,
We have a list containing names of countries. We need to find names of
I have a list containing four images. Each image should only have one active
I have a List containing several keywords. I foreach through them building my linq
I have a list containing a tuples and long integers the list looks like
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,

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.