• 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

Collections and the order of returning elements

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen this question:

Which of the following classes, will return elements in the order they were added (first in first out) when accessed via an iterator?

1 java.util.HashSet
2 java.util.Vector
3 java.util.LinkedHashSet
4 java.util.TreeSet

I thought the answer was 3, but the correct answer is 2 and 3. This made me wonder why, and I think that it must be like this that all lists (ArrayList, Vector and LinkedList) return elements in the added order, since they are indexed listed, and the linked collections (LinkedList, LinkedHashSet, LinkedHashMap) return them in the added order because they are linked. The other collections return them in a sorted order, TreeSet for example, or in a random order, like for example HashSet.

Is that correct?
[ August 04, 2007: Message edited by: Marc Wentink ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1 java.util.HashSet (you don't know the order of retrieval)
2 java.util.Vector (FIFO)
3 java.util.LinkedHashSet (FIFO)
4 java.util.TreeSet (Comparator decides the order)



Thanks,
[ August 04, 2007: Message edited by: Chandra Bhatt ]
 
Poop goes in a willow feeder. Wipe with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic