简体   繁体   中英

Methods with Different Signatures that Return the Intersection of Two Sets (Java)

I created a method that returns the intersection of two sets of values. The thing is that I want to use a different signature that uses only one arrayList within the method and 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 the other method with the different signature. How can I make this method to return the intersection of 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 
      }
 }

You cannot overload methods by their return value in Java - you'll have to rename one of them. Eg:

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

public static ArrayList intersectionArrayList(Group A, Group B) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM