• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Does entrySet method of Map guarantee the order on each call ?

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I just wanted to know, if i call entrySet on HashMap 100 times (just for example), then is there any guarantee to get the array of entries (entryArray) with same order everytime.

I have code below;



Here mySchedules is some Map<Integer,Object> object.

Thanks,
VijayS.


 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vijay saraf wrote:Hi All,

I just wanted to know, if i call entrySet on HashMap 100 times (just for example), then is there any guarantee to get the array of entries (entryArray) with same order everytime.



If you look at source code of HashMap you will find, that entrySet() returns java.util.Set implementation extending java.util.AbstractSet, and overriding iterator() method. Returned Iterator is extension of HashMap.HashIterator with next() method implementation returning nextEntry() of HashMap.HashIterator. This implementation simply uses table HashMap.Entry[] and returns next element of that table as nextEntry() return value. So if you have HashMap and iterate through its EntrySet elements it will always return them in the same order. Check yourself if HashMap modification (ie. removing elements) changes elements order in this table.

HashMap.EntrySet.toArray() method is implemented in java.util.AbstractCollection, and it is easy to check its javadoc, which says:

Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.


That's why the answer to your question is yes - this order is guaranteed (but again - check yourself what happens when you change elements of HashMap)

vijay saraf wrote:
Here mySchedules is some Map<Integer,Object> object.



So here you can't predict if order is guaranteed - it depends on specific implementation of java.util.Map, especiallly iterator() method implementation in Set implementation returned by entrySet() method implementation.

regards,
t.
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Thanks Tomasz !!!
I got it.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic