permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes order of elements returned by Iterator in Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "order of elements returned by Iterator in Set" Watch "order of elements returned by Iterator in Set" New topic
Author

order of elements returned by Iterator in Set

Padma Palepu
Greenhorn

Joined: Sep 12, 2002
Posts: 4
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.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Padma Palepu
Greenhorn

Joined: Sep 12, 2002
Posts: 4
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.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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?


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.



SCJP2. Please Indent your code using UBB Code
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
You're right Jose, the wording in the question is not correct.. Set should have been HashSet for such an answer to be right..
 
wood burning stoves
 
subject: order of elements returned by Iterator in Set
 
Similar Threads
how can be retrieve from any collection into our own order?
pls help me.
Another question from mock exam
TreeXxx VS PriorityQueue
Please explain me this!