Forums Register Login

java.util.LinkedHashSet

+Pie Number of slices to send: Send
java.util.LinkedHashSet

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 insertion order vs ordered by index?

thanks in advance ;)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Joseph.

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

thanks

+Pie Number of slices to send: Send
 

Glen Iris wrote:java.util.LinkedHashSet

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.

What are the correct answers?

 
1
+Pie Number of slices to send: Send
 

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



Yes. You are right.
+Pie Number of slices to send: Send
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1462 times.
Similar Threads
Collections and the order of returning elements
Picking the right Collection class Question.
most efficient way to remove duplicates from list?
K&B Chapter 7 Question 7
do i need install jdk1.4 to pratise java program??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:32:16.