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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:16:06+00:00 2026-06-18T02:16:06+00:00

I’ve experienced some really spooky TreeMap behavior and I’ve had some trouble narrowing down

  • 0

I’ve experienced some really spooky TreeMap behavior and I’ve had some trouble narrowing down a small test case, so bear with me.

I want to read a large number of key-value pairs into a Map, from a file provided at runtime. I’m using a custom key class. Later, when I go to pull entries back out, I find that one or more of them are missing. Using a debugger and some test cases, I’ve determined that the missing entry(ies) are definitely disappearing during the read phase, but I’m not sure what’s causing it.

Basically:

Map<MyKey,Double> map = new TreeMap<MyKey,Double>();
map.put(key1,value1);

// ... put another ~500 entries into the map ...

assertTrue(map.containsKey(key1)); // passes
if (!map.containsKey(keyN)) { 
    map.put(keyN, valueN); // this code executes
}
assertTrue(map.containsKey(key1)); // FAILS

…So essentially, adding a brand new key to the map causes an unrelated entry to fall out of it.

  • If I just add key1 and keyN alone, key1 remains in the map — the intervening 500 entries are somehow important
  • If I remove one or two arbitrary keys from 2..(N-1), key1 is still booted out when keyN is added
  • If I remove a large range of keys from 2..(N-1), key1 remains when keyN is added, but falls out when (say) keyQ is added, ~300 keys further down the line
  • Unfortunately the size of the map when keyN kicks out key1 is not the same as the size of the map when keyQ kicks out key1, so it’s probably not a limited-size issue
  • If I use a HashMap instead, key1 remains in the map
  • Custom key class MyKey uses the same logic for Comparable, equals, and hashCode.

I was initially using TreeMap because I expect to be using large datasets, and TreeMap is a little more memory-efficient. HashMap will be a fine alternative, but it’s still alarming to see TreeMap behave this way — anyone have thoughts on what’s going on here?

  • 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-06-18T02:16:07+00:00Added an answer on June 18, 2026 at 2:16 am

    TreeMap thinks of two entries being equal if, when compared, the result is 0. So if key1 and keyN ‘compare to’ 0, then key1 will be overwritten by keyN being put(). This is true even if !key1.equals(keyN). So while you may think that those two keys are not equal, and so inserting one should not overwrite the other, the TreeMap will think different if your equals and comparison functions are inconsistent with each other.

    Note that this behavior may vary depending on the number of elements in the map, because it depends on actually comparing two elements who’s compare method evaluates to 0. Basically, things will act, as you say, ‘spooky’.

    From the TreeMap javadocs:

    …map performs all key comparisons using its compareTo (or compare)
    method, so two keys that are deemed equal by this method are, from the
    standpoint of the sorted map, equal…

    and from the Comparable javadocs (thanks @Brian):

    It is strongly recommended (though not required) that natural
    orderings be consistent with equals. This is so because sorted sets
    (and sorted maps) without explicit comparators behave “strangely” when
    they are used with elements (or keys) whose natural ordering is
    inconsistent with equals. In particular, such a sorted set (or sorted
    map) violates the general contract for set (or map), which is defined
    in terms of the equals method.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.