Forums Register Login

Creating a Set

+Pie Number of slices to send: Send
Suppose we did not have Set API in the collection class and we need to create a set by ourself. So what would the approach be.

What i think we need to take care of just being able to add unique elements in the set,

But i see that in TreeSet there is a comparable/comparator used to compare the elements, how do i take care of it.

Any suggestions would be great.

Thanks,
+Pie Number of slices to send: Send
I'm a little confused, are you trying to make your own set class? The reason TreeSet uses the compare/comparator is because in addition to getting rid of duplicates, TreeSet also keeps all of it's data sorted, as if you were calling Collections.sort() on your set after every single time you added an object. It uses the compare method, or the comparator to see how to sort the data
+Pie Number of slices to send: Send
It is quite easy to create your own binary trees and use them as tree sets.
+Pie Number of slices to send: Send
Yes, if you are told to create your own Set class, how would you do that?
+Pie Number of slices to send: Send
I would start out by finding out the requirements, or specifications, for the Set class you had to write. For example you don't know whether you need a particular feature of TreeSet or not. So you should find that out.
+Pie Number of slices to send: Send
 

Jacob Sonia wrote:But i see that in TreeSet there is a comparable/comparator used to compare the elements, how do i take care of it.



There's optionally a Comparator. One ot the constructors to TreeSet allows you to pass in a Comparator object.

Now if you allow this you must use this comparator when you keep the TreeSet ordered, otherwise you're supposed to use the natural order of the objects stored in the TreeSet. The latter means you must assume the objects implement Comparable.
+Pie Number of slices to send: Send
If i want to create exactly like a hashset, what should be my approach
+Pie Number of slices to send: Send
 

Jacob Sonia wrote:If i want to create exactly like a hashset, what should be my approach



Exact?

I took all the interfaces implemented by HashSet and then I defined a new class MySet promising to implement those interfaces. I then let Eclipse add all missing methods. I also added the expected constructors from HashSet.

When you've implemented all these constructor and methods the MySet class will work exactly like a HashSet. Of course it's best to start small. Start with the default constructor and the add and the contains methods. That will constitute a little mini-HashSet.

I'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1273 times.
Similar Threads
Java doesn't provide any API to directly cast int[] to Integer[] ?
sortedSet with objects
open source code that can replace JTextPane
Maps and Sets
Finding "equal" elements in a List.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:00:50.