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

  • Home
  • SEARCH
  • 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 4060168
In Process

The Archive Base Latest Questions

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

Can someone explain why the HashMap acts like it does in this example: Simple

  • 0

Can someone explain why the HashMap acts like it does in this example:
Simple test that checks a hashmap for a key. Once in the constructor and once in ListDataListener intervallAdded method.

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import com.jgoodies.common.collect.ArrayListModel;

public class Test1 {

  private final Listener listener = new Listener();
  private final Map<List<?>, Object> parentByCollection = new HashMap<List<?>, Object>();

  public Test1(){
    ArrayListModel<Object> list = new ArrayListModel<Object>();

    list.addListDataListener(listener);

    parentByCollection.put(list, new Integer(10));

    // Test containsKey locally
    System.out.println("Item exists (locally):" + parentByCollection.containsKey(list));

    // Test containsKey via ListDataListener
    list.add(new Integer(20));
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    new Test1();
  }

  public class Listener implements ListDataListener{

    @Override
    public void intervalAdded(ListDataEvent e) {
      List<?> itemSource = (List<?>)e.getSource();

      System.out.println("Item exists (listener):" + parentByCollection.containsKey(itemSource));      
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
    }
  }
}

Why does the hashmap return false from the event but true from constructor when using containsKey?
Is there some java-generics “magic” I don’t know about here?

Edit:

Just found that ArrayList’s (which ArrayListModel extends) hashCode method assembles its hashcode from all its elements. Which means the hashCode changes with the items in the list.
So storing a ArrayList in a HashMap is not a good idea.

How can I solve this? Store the collections in a holder/container object instead?

  • 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-20T15:18:14+00:00Added an answer on May 20, 2026 at 3:18 pm

    I know you understand the problem now, but here is the explanation for other people:

    The HashMap uses the hashcode of the key, which in this case is the hashcode of the List.

    Looking at the javadoc of the hashcode method of List explains that the hashcode of the list depends on the contained elements in order to respect the contract between hashcode and equality.

    Due to the contract any subsequent modification of the list resulting in an equality change will also result in a hashcode change and thus the Hashmap will not be able to retrieve the initial list.

    The solution in this case is to use a reference, it will not change when elements are added or removed from the list. But a clone of the list (one that is equal to it) will not work !

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

Sidebar

Related Questions

Can someone explain this result to me. The first test succeeds but the second
Can someone explain ProxyFactoryBean in simple terms? I see this being quoted lot of
Can someone explain what this means? int (*data[2])[2];
Can someone please explain to me the static HashMap#hash(int) method? What's the justification behind
Can someone explain why this code doesn't work as expected? I would expect it
Can someone explain these two - Index Key Column VS Index Included Column? Currently,
Can someone explain why this code: import sys sys.path.append(C:\\WINDOWS\\system32) import clr clr.AddReferenceToFile(wiimotelib.dll) works fine
Can someone explain what is happening here? class Test(object): __getitem__ = getattr t =
Can someone explain to me why this command reports The syntax of the command
Can someone explain to me something about related fields. For example - How it

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.