Reputation: 66751
Does such a thing exist anywhere? Basically I see java has LinkedHashSet but no type of navigatable hash set?
Upvotes: 0
Views: 1186
Reputation: 11
You can use a TreeSet but all the operations in it are lg(n)
You can use a LinkedHashSet, which keeps a linked list on top of hashset, but it only maintains insertion ordering (first inserted will be first element in iterator), you cannot have natural or custom ordering
You could also use TreeSet+HashSet approach but two reference for each element will be kept and while add and remove would still be lg(n) the contains will become expected o(n)
choose wisely :)
Upvotes: 1
Reputation: 66751
I guess there's TreeMap which is...related but definitely not the same :)
Upvotes: 0
Reputation: 10020
By its very nature, a hash-based data structure is not ordered. You can write wrappers which supplement it with an additional data structure (this is more or less what LinkedHashMap
does). But while it makes some sense to keep a hash set and a list, in order to keep a good ordering, you would need a tree or similar data structure. But the tree can work as a set by itself, so you would essentially be duplicating the information (more than in the case of set plus list, which differ more than two different set implemnentations). So the best solution is to just use TreeSet
or another SortedSet
if you need order.
Upvotes: 1
Reputation: 49187
It's not a HashSet
, but as a descendant of Set
you have the TreeSet
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order
You can traverse the elements using the iterator
public Iterator iterator()
Returns an iterator over the elements in this set. The elements are returned in ascending order
Upvotes: 1