Two Laptop Bag
The moose likes Java in General and the fly likes Need Help in Implementing a Set Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help in Implementing a Set Interface" Watch "Need Help in Implementing a Set Interface" New topic

Need Help in Implementing a Set Interface

Yel Chan

Joined: Jul 29, 2010
Posts: 1
Hi I'm new to Java and I badly need help on how to implement the Set interface below:

public interface Set {

public Set union(Set S);

public void add(Object x);


I have this code:

public class Test implements Set{

public Set union (Set A){


public void add(Object x){


public static void main (String args[]){

Set A = new MySet();



Can you help me on how the methods should be implemented. I wanted to add an array to Set A and add values on that array. Then I'm going to pass those array to the union method. Thanks much.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Java api has some Set implementation[like HashSet,TreeSet] .there is also method called addAll for your union function. you dont need to implement your self.
what is MySet which in your code? you mean Test right?. apart from this if you want to implement Set data structure yourself. then you need to google much first
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

This conversation is also here:

Firstly, we're not talking about the java.util.Set interface, I assume we're talking about an interface used for teaching.

To use an interface:
1) create a class that implements the interface
2) either write all of the methods or declare the Class abstract. You will almost always want to implement all of the methods.

I'll get you started:
I agree. Here's the link:
subject: Need Help in Implementing a Set Interface
It's not a secret anymore!