This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Collections - the meaning of ORDERED

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"A List is ordered. This allows a List to maintain an order as objects are added and removed from the collection. This is not the same as sorted! Sorted objects form a hierarchy based on how the objects compare to each other, such as alphabetical sorting."
I really can't understand the meaning of "ORDERED".... I can add a element in any position by using the add(int index, Object element) method.... so it is not ordered, is it?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Renata fonseca:
I really can't understand the meaning of "ORDERED".... I can add a element in any position by using the add(int index, Object element) method.... so it is not ordered, is it?


It is ordered... The order of the list remains in exactly the order that you placed it in.
whether you used the list.add(Object) places the object at the end of the list
or the list.add(int, Object)places the object at a particular point in the list
When you call list.iterator() you will retrieve the objects in exactly the order you placed them in, whereas in a set object, you're not guaranteed what order you can retrieve the objects:

output:
Ordered List:
one
two
three
four
five
-------------
Unordered Set:
one
two
five
four
three
 
Renata fonseca
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!
Could you explain me when I'd use ordered and unordered interface?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Renata fonseca:
Could you explain me when I'd use ordered and unordered interface?


hmmmm I'm awful at coming up with examples... but I'll do my best (realize I'm stretching here, but work with me...)
Ordered Collection -->
Say you want to hold a set of directions in a collection (for some reason)... you want those directions to be retrieved in the EXACT same order as they were stored

If someone were to follow those directions.. it would be devestating if they weren't returned in the same order -- who knows where they'd end up!
Unordered collection -->
So.. you finally get to where you're going, and now you pull out your handy-dandy grocery set (note: not a grocery list, a grocery set):

It really doesn't matter what order the set is returned in -- you just need the information held in there.
------
Realize there are other advantages and disadvantages of using different collection classes (speed of access, allows duplicates?, indexing etc).
Thomas Paul wrote up a very nice series of articles on the different collection classes. Check them out if you have any further questions, it might help explain things better for you.
1- List Interface
2- Set Interface
3- Map Interface
4- Collections Class
 
Renata fonseca
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!!
 
What's wrong? Where are you going? Stop! Read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic