• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Am I guaranteed to get these in the same order?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both will be in same order. Even though, Hash Map - insertion order and sorting is not guaranteed.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic