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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:04:05+00:00 2026-05-28T20:04:05+00:00

How can I use contains method on a list that contains HashMaps so that

  • 0

How can I use contains method on a list that contains HashMaps so that it returns true when I ask whether or not the list contains a specified HashMap

For Example:

    List ssibenefit = new ArrayList();
    HashMap map = new HashMap();
    map.put("one", "1");
    ssibenefit.add(map);
    HashMap map4 = new HashMap();
    map4.put("one", "1");

    System.out.println("size: " + ssibenefit.size());
    System.out.println("does it contain orig: " + ssibenefit.contains(map));
    System.out.println("does it contain new: " + ssibenefit.contains(new HashMap().put("one", "1")));

should return:

size: 1
does it contain orig: true
does it contain new: true

but in this case the actual output is

size: 1
does it contain orig: true
does it contain new: false

Update

Sorry, I’ve updated the question. As I was posting the question, I realized what I wanted to do and what I was asking were not the same thing.

  • 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-28T20:04:06+00:00Added an answer on May 28, 2026 at 8:04 pm

    I’m afraid the answers you’ve been given so far (prior to Vlad’s answer) are all complete rubbish (which is surprising, because this is pretty basic stuff!).

    The reason your test prints false where you would like to see true is that put("one", "1") returns the previous value of the mapping, not the map you’ve just manipulated. So, your call to ssibenefit.contains is asking if the list contains null. Which it doesn’t.

    On the other hand, if you rewrite your test:

    HashMap newMap = new HashMap();
    newMap.put("one", "1");
    System.out.println("does it contain new: " + ssibenefit.contains(newMap));
    

    You will see that it prints true.

    If you read the javadoc for List.contains, you’ll see that it

    returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))

    So, it works by equality, not identity, as others have asserted. And if you read the javadoc for Map.equals, you’ll see that it

    Returns true if the given object is also a map and the two maps represent the same mappings

    So, equality is defined as equality of content, not identity. Putting those two together, you can see that what you’re trying to do must work on any compliant implementation of Java.

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

Sidebar

Related Questions

How can I use Membership.GeneratePassword to return a password that ONLY contains alpha or
How can I use find_if with a std::list if the list contains structs? My
I've got a few controllers here at work that contain methods I can use
You can use a standard dot notation or a method call in Objective-C to
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
We have a database library in C# that we can use like this: DatabaseConnection
I'm interested to know if there is an interface that I can use to
I am trying to make a generic method that can accept any object, in
Why is there a List<T>.Contains(T) method but no List<T>.Find(T) method? Only the Find s
How can I force a shrink of a DataTable and/or List so that I

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.