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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:07:30+00:00 2026-05-14T00:07:30+00:00

I have a Map that uses a Set for the key type, like this:

  • 0

I have a Map that uses a Set for the key type, like this:

Map<Set<Thing>, Val> map;

When I query map.containsKey(myBunchOfThings), it returns false, and I don’t understand why. I can iterate through each key in the keyset and verify there is a key that (1) has the same hashCode, and (2) is equals() to myBunchOfThings.

System.out.println(map.containsKey(myBunchOfThings)); // false.
for (Set<Thing> k : map.keySet()) {
  if (k.hashCode() == myBunchOfThings.hashCode() && k.equals(myBunchOfThings) {
     System.out.println("Fail at life."); // it prints this.
  }
}

Do I just fundamentally misunderstand the contract for containsKey? Is there a secret to using sets (or more generally, collections) as keys to maps?

  • 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-14T00:07:30+00:00Added an answer on May 14, 2026 at 12:07 am

    Key should not be mutated while used in the map. The Map java doc says:

    Note: great care must be exercised if
    mutable objects are used as map keys.
    The behavior of a map is not specified
    if the value of an object is changed
    in a manner that affects equals
    comparisons while the object is a key
    in the map. A special case of this
    prohibition is that it is not
    permissible for a map to contain
    itself as a key. While it is
    permissible for a map to contain
    itself as a value, extreme caution is
    advised: the equals and hashCode
    methods are no longer well defined on
    a such a map.

    I knew this issue, but never made the test until now. I elaborate then a bit more:

       Map<Set<String>, Object> map  = new HashMap<Set<String>, Object>();
    
       Set<String> key1 = new HashSet<String>();
       key1.add( "hello");
    
       Set<String> key2 = new HashSet<String>();
       key2.add( "hello2");
    
       Set<String> key2clone = new HashSet<String>();
       key2clone.add( "hello2");
    
       map.put( key1, new Object() );
       map.put( key2, new Object() );
    
       System.out.println( map.containsKey(key1)); // true
       System.out.println( map.containsKey(key2)); // true
       System.out.println( map.containsKey(key2clone)); // true
    
       key2.add( "mutate" );
    
       System.out.println( map.containsKey(key1)); // true
       System.out.println( map.containsKey(key2)); // false
       System.out.println( map.containsKey(key2clone)); // false (*)
    
       key2.remove( "mutate" );
    
       System.out.println( map.containsKey(key1)); // true
       System.out.println( map.containsKey(key2)); // true
       System.out.println( map.containsKey(key2clone)); // true
    

    After key2 is mutated, the map does not contain it anymore. We could think that the map “indexes” the data when it’s added and we would then expect that it still contains the key2 clone (line marked with *). But funny enough, this is not the case.

    So, as the java doc says, keys should not be mutated otherwise the behavior is unspecified. Period.

    I guess that’s what happens in your case.

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

Sidebar

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.