Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.LinkedHashSet

 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;)
 
Joseph Arnold
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, QuoteYourSources
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic