File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating a Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a Set" Watch "Creating a Set" New topic
Author

Creating a Set

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
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,
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

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


www.mormon.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
It is quite easy to create your own binary trees and use them as tree sets.
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Yes, if you are told to create your own Set class, how would you do that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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.
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
If i want to create exactly like a hashset, what should be my approach
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a Set