• 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

order of elements returned by Iterator in Set

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q)10 in Dan's collections mock exam 1
(http://www.danchisholm.net/nov04/topic/collections1.html)
The answer is given as 'c'.But I guess the TreeSet(option d) is also correct.The reason is the collection can be added to a TreeSet and the iterator returns it in ascending order.Since the iterator in previous TreeSet also returns it in ascending order,the requirement will be satisfied.
Any ideas for the answer.
Thanks.
 
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
can you post the text of the question including any source code?
--> you can use the [code] tags to list the code segments and preseve the whitespace.
[ November 14, 2002: Message edited by: Jessica Sant ]
 
Padma Palepu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jessica,
it is as follows:
Q)Suppose that you would like to create an instance of a new Set that has an iteration order that is the same as the iteration order of an existing instance of a Set. Which concrete implementation of the Set interface should be used for the new instance?
a. The answer depends on the implementation of the existing instance.
b. HashSet
c. LinkedHashSet
d. TreeSet
e. None of the above.
Answer:
option c.
reason:
The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The iteration order of a LinkedHashSet is determined by the order in which elements are inserted. When a new LinkedHashSet is created by passing a reference to an existing Collection to the constructor of a LinkedHashSet, the Collection.addAll method will ultimately be invoked. The addAll method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance of the new LinkedHashSet. Since the iteration order of the LinkedHashSet is determined by the order of insertion, the iteration order of the new LinkedHashSet must be the same as the interation order of the old Collection.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padma, you're mistaken.. see:
1- HashSet: iteration order is NOT predictable, since *I guess* it depends on the hashCode() int value of the objects, or it uses an algorithm that you need not know for the exam, all you need to know is that it's unpredictable. So HashSet collection are actually NOT ordered.
2- LinkedHashSet: ARE ordered by the order elements were inserted, you can also instruct it to order elements by which one was last accessed - this is important - so Iteration order is according - by default - to the insertion order.
3- TreeSet: is not only ordered but it is Sorted, Sorted means it iterates the elements ascendingly - if this is how you spell it -according to their "Natural Order" and NOT by the order they were inserted. Natural Order depends on the type of the Objects inserted, for instance String elements will be ordered alphabetically, Integer Objects will be ordered according to their mathimatical values - remember, you can't insert int into a collection, since it's a premitive, not an Object -. More over you can instruct the TreeSet to sort elements in your own specifications.
So for instance if HashSet will iterate elements like "four", "two", "five", "one" then if this is inserted - with this order - to a LinkedHashSet, it will iterate them the same way... BUT a TreeSet will "sort" them as - ok a b c d e f hehehe - "five", "four", "one", "two"
Got the picture?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with all that Alfred said but I think this code qualifies TreeSet also to comply with...


Suppose that you would like to create an instance of a new Set that has an iteration order that is the same as the iteration order of an existing instance of a Set.


 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Jose, the wording in the question is not correct.. Set should have been HashSet for such an answer to be right..
 
All of the world's problems can be solved in a garden - Geoff Lawton. 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