-1

I'm trying to find a way to display my intersection of 2 sets of array containing elements. I couldn't find a way to display my intersection

This is a method to find the intersection of arrays

enum Month {
    
    Jan ("Janurary","01"),
    Feb ("Feburary","02"),
    Mar ("March","03"),
    Apr ("April","04"),
    May ("May","05"),
    Jun ("June","06"),
    Jul ("July","07"),
    Aug ("August","08"),
    Sep ("September","09"),
    Oct ("October","10"),
    Nov ("November","11"),
    Dec ("December","12");
    
    private final String day; //day of the month
    private final String date; //name of the date
    
    //enum constructor 
    Month(String date, String day)
    {
        this.date=date;
        this.day=day;
    }
    
    //accessor for field day
    public String getDay() {
        return day;
    }
    //accessor fpr field month
    public String getDate() {
        return date;
    }
}

class Set{
...
...
...     
public void intersection(Set otherSet)
        {
              for(int x=0; x< otherSet.s.size(); x++)
                {
                   if(belongTo(otherSet.s.get(x)))
                   {
                       addElement(otherSet.s.get(x));
                   }
                }
    }

This method is to get a random set of element in a array. 2 different class, class Set have other methods to calculate other stuffs. The main class is to display it

class Test{
   ... 
   ...
   ...
    private static Set getASet()
        {
            //Set s=new Set()   
            Set s=new Set();
            int rand=(int)(Math.random()*12);
            for(int i=0;i<=rand;i++)
            {
                s.addElement(getAnElement());
            }
            return s;
            
        }
}
    private static void intersectionExample() {
            
                Set A=getASet();
                Set B=getASet();
                Set u=new Set();
                u= A.intersection(B);
                System.out.println("Given sets");
                System.out.printf("A={%s}%n", A.toString());
                System.out.printf("B={%s}%n",B.toString());
                System.out.printf("Intersection of A and B = {%s}", u.toString());
            }
  • Have you tried this: `A.retainAll(B)`? – Mr. Polywhirl Oct 15 '20 at 16:05
  • Does this answer your question? [How to calculate the intersection of two sets?](https://stackoverflow.com/questions/8882097/how-to-calculate-the-intersection-of-two-sets) – Mr. Polywhirl Oct 15 '20 at 16:06
  • @Mr.Polywhirl I need to use the method intersection, so if I use A.intersection(B), but I need to find something to store it in, in oder to display – Ng Leng Poh Oct 15 '20 at 16:08
  • The [`retainAll`](https://docs.oracle.com/javase/9/docs/api/java/util/Set.html#retainAll-java.util.Collection-) method is an intersection. – Mr. Polywhirl Oct 15 '20 at 16:10
  • @Mr.Polywhirl so if I A.retainAll(B), how do I store it into a object or variable in order to display it at printf – Ng Leng Poh Oct 15 '20 at 16:11
  • The answer to your question is linked in the duplicate question. Create a new set based on `A` and then retain only things found in `B`. – Mr. Polywhirl Oct 15 '20 at 16:12
  • @Mr.Polywhirl I think the asker has made his own custom class called `Set` and isn't using the `java.util.Set` interface/classes at all. otherwise the calls he does on his sets make absolutly no sense like `otherSet.s.get(x)` doesn't look like `otherSet` is of the type `java.util.Set`. This is of course pretty confusing and I was just about to tell the asker that he shouldn't use Set as a raw type when i saw all those weird calls he does on the Set class he uses. – OH GOD SPIDERS Oct 15 '20 at 16:12

1 Answers1

0
public class Test {

    private static Integer getAnElement() {
        return (int) (Math.random() * 10);
    }

    private static Set getASet() {
        //Set s=new Set()   
        Set<Integer> s = new HashSet();
        int rand = (int) (Math.random() * 12);
        for (int i = 0; i <= rand; i++) {
            s.add(getAnElement());
        }
        return s;

    }
    
    private static Set intersection(Set A , Set B) { 
        Set<Integer> s = new HashSet();
        Iterator<Integer> it = A.iterator();
        while(it.hasNext()){
            Integer i = it.next();
            if(B.contains(i)) s.add(i);
        }
        return s;
    }

    public static void main(String[] args) {
        Set A = getASet();
        Set B = getASet();
        Set u = intersection(A, B);
        System.out.println("Given sets");
        System.out.printf("A={%s}%n", A.toString());
        System.out.printf("B={%s}%n", B.toString());
        System.out.printf("Intersection of A and B = {%s}%n", u.toString());
    }

}

output:

Given sets
A={[0, 3, 7, 8]}
B={[0, 1, 3, 4, 6, 7, 9]}
Intersection of A and B = {[0, 3, 7]}
aziz k'h
  • 775
  • 6
  • 11
  • can't be done because its a void so it will underline the intersection – Ng Leng Poh Oct 15 '20 at 16:23
  • intersection is a method in class Test and i just call it like this `intersection(A, B);` it works fine, i test it in my computer. And you can change type of sets elements if you went – aziz k'h Oct 15 '20 at 16:26