Methods with different signatures that return the intersection of two sets (Java)

I have created a method that returns the intersection of two sets of values. The thing is, I want to use a different signature that only uses one arrayList method inside the method, not all of them.

public class Group <T>
{     
  ArrayList<T> w = new ArrayList<T>();

  //Here I have the add and remove methods and a method  that returns 
  //false if the item is not in the set and true if it is in the set

  public static ArrayList intersection(Group A, Group B) 
  {
    ArrayList list = new ArrayList();
    ArrayList first = (ArrayList) A.w.clone();
    ArrayList second = (ArrayList) B.w.clone();


    for (int i = 0; i < A.w.size(); i++) 
    {
        if (second.contains(A.w.get(i))) 
        {
            list.add(A.w.get(i));
            second.remove(A.w.get(i));
            first.remove(A.w.get(i));
        }
    }
    return list;
  }
}

      

This is a different method with a different signature. How can I get this method to return the intersection of the two sets if the signature is different from the method shown above?

public class Group <T>
{
   ArrayList<T> w = new ArrayList<T>();

   public static <T> Group<T> intersection(Group <T> A, Group <T> B)
   {
       Group<T> k= new Group<T>();


     return  k;
    }
}

public class Main
{
     public static void main(String [] args)
     {
         Group<Integer> a1 = new Group<Integer>();
         Group<Integer> b1 = new Group<Integer>();
         Group<Integer> a1b1 = new Group<Integer>();


         //Here I have more codes for input/output 
      }
 }

      

+3


source to share


1 answer


You cannot overload methods by their return value in Java - you will have to rename one of them. For example:



public static <T> Group<T> intersectionGroup(Group <T> A, Group <T> B)

public static ArrayList intersectionArrayList(Group A, Group B) 

      

+3


source







All Articles