Find common elements - Java Program

Q. Write a program to compare two sets and find the common elements from both sets.

Answer:

The program below shows how to compare the two list and find the common elements between them. We are using iterator method and retainAll() method to find the common element.

Common_Elements.java

import java.util.*;
class Common_Elements
{
        public static void main(String[] args)
        {
                String str1[] = {"One", "Two", "Three", "Four", "Five", "Six"};
                String str2[] = {"Two", "Four", "Six", "Eight", "Ten"};
                //Using iterator methods to find common elements
                System.out.println("Using iterator methods to find common elements");
                HashSet<String> hs = new HashSet<String>();
                for (int i=0; i< str1.length; i++)
                {
                        for (int j=0; j< str2.length; j++)
                        {
                                if (str1[i].equals(str2[j]))
                                {
                                        hs.add(str1[i]);
                                }
                        }
                }
                System.out.println(hs);
                System.out.println("**=====================**=====================**");
                System.out.println("Using retailAll() method to find common elements.");
                HashSet<String> set1 = new HashSet<>(Arrays.asList(str1));
                HashSet<String> set2 = new HashSet<>(Arrays.asList(str2));
                set1.retainAll(set2);
                System.out.println(set1);
                System.out.println("**=====================**=====================**");
        }
}


Output:

common elements