It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How is order working in a Collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How is order working in a Collection?" Watch "How is order working in a Collection?" New topic
Author

How is order working in a Collection?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

This class is ordered by name but what does it happen when various objects have the same name?



Result:

Which object is the first? Depends on value, hashcode..?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 32328
    
213

It depends on the original order of the list which affects the order compare to is run. In other words, you shouldn't rely on the order. It isn't guaranteed and future implementations of the JDK could change it.


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How is order working in a Collection?
 
It's not a secret anymore!