aspose file tools*
The moose likes Java in General and the fly likes Am I guaranteed to get these in the same order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Am I guaranteed to get these in the same order?" Watch "Am I guaranteed to get these in the same order?" New topic
Author

Am I guaranteed to get these in the same order?

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Imagine that I have the following code:



Am I guaranteed that the keys and vals arrays are in the same exact order?

Note: I do NOT care whether they're in alphabetical order. I simply care, if "a" is in position 3 of array keys then "1" MUST be in position 3 of array vals. Is this the case? Is it guaranteed to be so? (assuming I synchronize the map and thus protect against any changes to the map)
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Yes, both will be in same order. Even though, Hash Map - insertion order and sorting is not guaranteed.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's a toughie. The AbstractMap class documentation for keySet() and values() both say that the returned collection is a thin wrapper over the collection returned by entrySet(), suggesting that the iterators you get from both of these will be wrappers over the iterator returned by entrySet().iterator(). It doesn't specifically say this, but it very strongly implies the property you desire.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As far as I can tell, this is always true in practice (assuming there are no modifications to the Map while you're calling the other methods, of course), but is not actually guaranteed anywhere. Ultimately I think you'd be better off creating the arrays yourself, to be sure they're both in the same order:
Though this also sort of begs the question, why do you need these two separate arrays, anyway? In general I would think that whatever you need from the arrays could also be gotten from the map. But if you do need the separate arrays for some reason, OK.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: The AbstractMap class documentation for keySet() and values() both say that the returned collection is a thin wrapper over the collection returned by entrySet(),

It says that this implementation (in AbstractMap) returns wrappers. That's their way of saying that overrides may do something completely different. HashMap overrides these methods, and is under no obligation to use thin wrappers over the entrySet(). Even though in reality, it still does exactly that.
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Jim Yingst:

Though this also sort of begs the question, why do you need these two separate arrays, anyway? In general I would think that whatever you need from the arrays could also be gotten from the map. But if you do need the separate arrays for some reason, OK.


OK, I'll have to do this (create the arrays myself). And the reason I'm converting them to arrays is that I'm coding against some stupid (oops, did I say that?) API that only accepts the two arrays DESPITE passing me back a Map of the values. Argh!
[ November 08, 2006: Message edited by: Dan Bizman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Am I guaranteed to get these in the same order?