• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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 ]
 
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic