Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  TreeMap wierdness  (Read 688 times)
0 Members and 1 Guest are viewing this topic.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Posted 2010-10-21 15:53:20 »

This one has me baffled
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public static void main( String[] args )
{
   Map<Integer, String> map = new TreeMap<Integer, String>();

   String[] strings = new String[] { "foo", "bar", "baz", "flub" };

   for( int i = 0; i < strings.length; i++ )
   {
      map.put( new Integer( i ), strings[ i ] );
   }

   boolean fail = true;

   String toRemove = fail ? strings[ 1 ] : strings[ 2 ];

   Iterator<Map.Entry<Integer, String>> iter = map.entrySet().iterator();
   while( iter.hasNext() )
   {
      final Map.Entry<Integer, String> e = iter.next();
      final int index = e.getKey().intValue();
      final String s = e.getValue();

      if( s == toRemove )
      {
         iter.remove();

         if( index != e.getKey().intValue() )
         {
            System.out.println( "wat. " + index + " != "
                  + e.getKey().intValue() );
         }
         else
         {
            System.out.println( "as expected" );
         }
      }
   }
}


If "fail" is true, the Map.Entry somehow changes its key value when I remove it from the map. I don't feel that this is the correct behaviour and it's certainly not made clear enough in the docs if it is intended, but I can sort of see how it might happen.

However, if "fail" is false, then everything works as I expect even though the only difference is which entry is removed.

To put it mildly,  Huh

Someone put me out of my puzzled misery and point me to the docs that explain why this is so.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-10-21 16:58:38 »

I think it's fair to assume that after you remove an entry, the values in it are 'undefined'. That the TreeMap reuses the entry should not have been the case, as it reveals the implementation in the public API, but the alternative would have been not to reuse it, which might cause a serious performance impact.

I base this on absolutely nothing.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #2 - Posted 2010-10-21 17:22:16 »

I think you're right.

A cautionary tale then. Always assume that the key value will change, store it at the first opportunity.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #3 - Posted 2010-10-21 17:24:42 »

I do believe that this is somewhere in the javadocs. It always claims that entry etc are "backed" by the map and changes in the map are reflected in the "collection" instances that are backed by the map.

However i can't find where in the Java doc right now.

I have no special talents. I am only passionately curious.--Albert Einstein
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (24 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!