why aren't there any indexable Sets?
Just wondering why there are no indexable Sets. Take a List, with it's get(index i)
and set(index i, Object o)
Why couldn't a sorted Set have the same getter/setter? A LinkedHashSet sorts elements according to insertion otder, so some sort of numerical indexing is apparently used internally?
The reason I'm asking is that Struts seems to require an indexable Collection when updating values in the collection's elements. So having a Collection with unique elements does unfortunately not seem to work very well with Struts if I want to be able to update the elements, not just displaying them
cheers,
pj
> Just wondering why there are no indexable Sets. Take
> a List, with it's get(index i)
and
> set(index i, Object o)
Why couldn't a
> sorted Set have the same getter/setter?
Because having a set that takes an index doesn't make sense for a set. E.g A HashSet is not sorted, and a TreeSet shouldn't allow you to add an element at the wrong index.
> A LinkedHashSet sorts elements according to insertion
> otder, so some sort of numerical indexing is
> apparently used internally?
No. the LinkedHashMap is a HashMap which also puts the keys in a linked list. It doesn't sort anything.
Kaj
thanks for your reply! I still think that a LinkedHashSet (not LinkedHashMap) could use an index, though. No risk of adding elements at the wrong index there (or am I wrong?)/pj
> thanks for your reply! I still think that a
> LinkedHashSet (not LinkedHashMap) could use an index,
> though. No risk of adding elements at the wrong index
> there (or am I wrong?)
Yes there is a risk at adding elements at the wrong index. A HashSet is backed by a HashMap, but the most important thing is that a HashSet and HashMap uses the hashCode of they key to know where to insert an object. They aren't inserted in sequence.
Kaj
A LinkedHashSet is based on a hash table and a doubly-linked list and it is in fact sorted according to insertion order (http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedHashSet.html).
Maybe the question boils down to why we need to implement a hashCode() for a LinkedHashSet at all? It does not seem to have anything to say for where the element is inserted (as it is inserted at the end). I guess it needs a hashCode since it is based on a hash table. But how it is based on a hash table, I do not understand :-)
I guess, an index must be provided from some indexable structure, like an array. Most probably, an array is the ONLY structure which provides an index, and only components based on an array can use this.
Most probably, they could have implemented a custom index just for this class, but apparently they did not. However, they included an iterator. So, the new for loop should make use of this...
> A LinkedHashSet is based on a hash table and a
> doubly-linked list and it is in fact sorted according
> to insertion order
> (http://java.sun.com/j2se/1.4.2/docs/api/java/util/Lin
> kedHashSet.html).
You are misusing (or at least stretching) the term 'sorted' here. It's not sorted. It maintains two collections, a hashset and a linked list.
> Maybe the question boils down to why we need to
> implement a hashCode() for a LinkedHashSet at all? It
> does not seem to have anything to say for where the
> element is inserted (as it is inserted at the end). I
> guess it needs a hashCode since it is based on
> a hash table. But how it is based on a hash table, I
> do not understand :-)
It's a hash table and a linked list. I think the real question is why we need this class in the JDK at all. It's merely a mixin of two classes. You can easily implement this yourself and get whatever behavior you need.
The most likely reason that it doesn't support indexed access is indexed access on linked lists is highly inefficient. If you need to combine the fast lookup features of hashing with insertion order and indexed access, you can combine ArrayList and HashSet into a custom collection.
Maybe the question boils down to why we need to implement a hashCode() for a LinkedHashSet at all?
You missed the point: LinkedHashSet works just like a HashSet, it needs hashing for efficiency. It IS primarily a HashSet, additionally it allows some access similar to a LinkedList. But any lookup works just like a HashSet lookup, etc.
Btw., it would be quite non-trivial to implement LinkedHashSet using two Collections, as you need the LinkedHashSet.Entry to be chained rather than just a separate List.