yiyi83
asked on
TreeSet and HashSet
I have a class, SortedHashSet.
It is supposed to incorporate both TreeSet and HashSet. The class is as below:
import java.io.*;
import java.lang.*;
import java.util.*;
class SortedHashSet<T> extends TreeSet<T>
{
}
I have to override the add function such that it will maintain a TreeSet and a HashSet.
Can anyone advise me how it can be done?
For HashSet, I will need to declare a variable of type, HashSet. How about TreeSet?
Thanks in advance.
Regards
It is supposed to incorporate both TreeSet and HashSet. The class is as below:
import java.io.*;
import java.lang.*;
import java.util.*;
class SortedHashSet<T> extends TreeSet<T>
{
}
I have to override the add function such that it will maintain a TreeSet and a HashSet.
Can anyone advise me how it can be done?
For HashSet, I will need to declare a variable of type, HashSet. How about TreeSet?
Thanks in advance.
Regards
This link will give you how to use all three HASHSET, LINKEDHASHSET, AND TREESET. You
can see the add methods of all three
http://java.sun.com/developer/JDCTechTips/2002/tt1105.html
can see the add methods of all three
http://java.sun.com/developer/JDCTechTips/2002/tt1105.html
ASKER
Hi,
Just a general idea.
I will be maintaining one HashSet and one TreeSet. During iteration, I will call the TreeSet. During searching, I will call the HashSet.
However, I am just wondering if anyone can tell me given that I extends TreeSet and contains HashSet, how do I program for the add(Object e) to add an item to both TreeSet and HashSet?
Hope that will give a fair idea.
Thanks.
Just a general idea.
I will be maintaining one HashSet and one TreeSet. During iteration, I will call the TreeSet. During searching, I will call the HashSet.
However, I am just wondering if anyone can tell me given that I extends TreeSet and contains HashSet, how do I program for the add(Object e) to add an item to both TreeSet and HashSet?
Hope that will give a fair idea.
Thanks.
>>I will be maintaining one HashSet and one TreeSet.
Why?
Why?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why would you extend TreeSet and not simply use one? A TreeSet is *already* a SortedSet
You can of course, have your own class that maintains a HashSet and a TreeSet, both pointing to the same objects (so the same objects will have 2 references, one in the TreeSet and one in the HashSet). But what's the use?
ASKER
Purpose:
So that it will optimize the search and iteration of items in the TreeSet or HashSet.
Hi Hoomany,
I want to overrides the add function of both TreeSet and HashSet. Hence, within my own add function, I cannot make use of that of theirs.
Regards
So that it will optimize the search and iteration of items in the TreeSet or HashSet.
Hi Hoomany,
I want to overrides the add function of both TreeSet and HashSet. Hence, within my own add function, I cannot make use of that of theirs.
Regards
>>So that it will optimize the search ...
When you say 'search' what do you mean? The entries are accessed by key ...
When you say 'search' what do you mean? The entries are accessed by key ...
> I want to overrides the add function of both TreeSet and HashSet
so you need to inherit your own class from HashSet and override the default add method
then use that derived class instead of HashSet within SortedHashSet class
and call when you call add, the overridden method will be invoked
public boolean add(T o) {
return super.add(o) & derivedHashSet.add(o);
}
so you need to inherit your own class from HashSet and override the default add method
then use that derived class instead of HashSet within SortedHashSet class
and call when you call add, the overridden method will be invoked
public boolean add(T o) {
return super.add(o) & derivedHashSet.add(o);
}
>>So that it will optimize the search ...
I think he/she wants both optimized insertion and ordered iteration
I think he/she wants both optimized insertion and ordered iteration
ASKER
Hi,
Search referring to finding out if an item exists within the HashSet.
Basically, I want to achieve O(1) searching and O(n) sorted iteration.
O(1) searching is achieved through HashSet.
O(n) sorted iteration is achieved through TreeSet.
My class is inheriting from TreeSet. Hence, I am containing the HashSet.
Search referring to finding out if an item exists within the HashSet.
Basically, I want to achieve O(1) searching and O(n) sorted iteration.
O(1) searching is achieved through HashSet.
O(n) sorted iteration is achieved through TreeSet.
My class is inheriting from TreeSet. Hence, I am containing the HashSet.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks all for the responses. I been busy trying out the different suggestions. They are all helpful.
Sorry for the lateness in awarding the points. Thank you.
Regards
Thanks all for the responses. I been busy trying out the different suggestions. They are all helpful.
Sorry for the lateness in awarding the points. Thank you.
Regards
hash set does not guarantees the order of iteration
on the other hand adding an elemnet to TreeSet is O(log n) but HashSet is O(1) (Constant time)
how is it possible to have a sorted hash set ?
if you want to be able to iterate through the set in ascending order, try
Set sortedSet= new TreeSet( yourHashSet );