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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:56:39+00:00 2026-05-23T09:56:39+00:00

It puzzles me how the following segment can lead to a null value of

  • 0

It puzzles me how the following segment can lead to a null value of the Boolean mandatory, although it is not null at the corresponding key in the actual hashtable:

for (List<List<A>> a : hashMap.keySet()) {  
    Boolean mandatory = hashMap.get(a);
}
  • 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-23T09:56:40+00:00Added an answer on May 23, 2026 at 9:56 am

    A HashMap will return null if the key specified is not bound to a value.

    Issue is almost certainly that comparison op on a — a List — against keys is failing.

    Let me guess: are you modifying these lists (the key object) after you have called a put? Did you remove all entries in one of the keys? Remember an empty list is equal to all empty ArrayLists. Further remember that List.equals() compares list content (one by one) to test equality.

    package sof_6462281;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Demonstrate the fact that the Map uses key.equals(k) to
     * test for key equality.  Further demonstrate that it is a 
     * very bad idea to use mutable collections are keys to maps.
     */
    public class ListAsKey {
        public static void main(String[] args) {
            Map<List<A>, Boolean>  map = new HashMap<List<A>, Boolean>();
    
            List<A> alist = new ArrayList<A>();
            map.put(alist, true);
            for (List<A> a : map.keySet()) {
                Boolean b = map.get(a);
                System.out.format("\t%s(ArrayList@%d) => %s\n",a, a.hashCode(), map.get(a)); 
            }
    
            // you changed your list after the put, didn't you?
            alist.add(new A());
            for (List<A> a : map.keySet()) {
                Boolean b = map.get(a);
                System.out.format("\t%s(ArrayList@%d) => %s\n",a, a.hashCode(), map.get(a)); 
            }
    
            alist.clear();
            for (List<A> a : map.keySet()) {
                Boolean b = map.get(a);
                System.out.format("\t%s(ArrayList@%d) => %s\n",a, a.hashCode(), map.get(a)); 
            }
        }
        public static final class A { /* foo */ }
    }
    

    Results:

    [](ArrayList@1) => true
    [sof_6462281.ListAsKey$A@4b71bbc9](ArrayList@1265744872) => null
    [](ArrayList@1) => true
    

    edit: added more ops to above and added console out.

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

Sidebar

Related Questions

I am a little puzzled why the following is not working, button_ONE does a
Can anyone please explain what the following code checks for? I can make no
I have the following (maybe common) problem and it absolutely puzzles me at the
In the following code, why does 2 give output but not 3? The removechars
I thought I had references in AS3 figured out, but the following behavior puzzles
I am learning some COM code and the following code puzzled me. STDMETHODIMP _(ULONG)
I'm a bit puzzled about the conditional operator. Consider the following two lines: Float
I have a list of puzzles that are tagged with specific Themes. Think questions
I'm using NetBeans to develop some simple applications to solve puzzles . Upon launching
This question relates to those parts of the KenKen Latin Square puzzles which ask

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.