• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Am I guaranteed to get these in the same order?

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, both will be in same order. Even though, Hash Map - insertion order and sorting is not guaranteed.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic