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.
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 ]
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.
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?