File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.util.LinkedHashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java.util.LinkedHashSet" Watch "java.util.LinkedHashSet" New topic
Author

java.util.LinkedHashSet

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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 ;)


OCPJP 6, OCMJD
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
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.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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?
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Please, QuoteYourSources
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.LinkedHashSet