which of the following is not true of the java.util.LinkedHashSet class and why?
1 - it allows you to grow or shrink its size.
2 - it provides indexed access to its elements.
3 - its methods are not synchronized.
I am fairly certain that the answer is 2 and that the reason is that it is ordered by insertion order and not index. If this is the case what is the difference between being:
Ordered by index is similar to contents in an array. Here retrieval of elements from the collection is based on the index. You can add or remove elements from any indexed position whereas in the case insertion order, you cannot access elements in any order. Here retreival of elements is similar to that of a stack or a queue.
So,using this (very crude) pseudo code, are the following statements correct?
ordered by index means: if i ask for collection[0] i will get the element at zero, it may or may not have been the first element added to the collection.
ordered by insertion order means: if i ask for collection[0] i will get the first element that was added to the set. the first element added to the set will always reside in collection[0].
which of the following is not true of the java.util.LinkedHashSet class and why?
1 - it allows you to grow or shrink its size.
2 - it provides indexed access to its elements.
3 - its methods are not synchronized.
Hi, Glen. I want to check answer with you to quiz myself.
1. is correct as you can add or remove from linked hash set.
2. is incorrect because you cannot use get(0), get(1) to retrieve each element, unlike linked list.
3. is correct as I checked the open source, the LinkedHashSet overrides the methods in HashSet. Those methods are not synchronized.
So far, as I remember K&B's book, only Vector has synchronized methods, which is slow in performance. That is why Vector is an obselete class based on some IDE such as NetBean Platform.
ordered by index means: if i ask for collection[0] i will get the element at zero, it may or may not have been the first element added to the collection.
ordered by insertion order means: if i ask for collection[0] i will get the first element that was added to the set. the first element added to the set will always reside in collection[0].