Q:

Java Program to compare two sets and retain elements which are same

0

Program to compare two sets and retain elements which are same.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
public class demo1 
{
 public static void main(String[] args)
 {
   HashSet<String> hash_set = new HashSet<String>();
   hash_set.add("C#");
   hash_set.add("C++");
   hash_set.add("Java");
   hash_set.add("Python");
   hash_set.add("Perl");
   hash_set.add("Ruby");
   System.out.println("Frist HashSet content: "+hash_set);
       
   HashSet<String>hash_set1 = new HashSet<String>();
   hash_set1.add("C#");
   hash_set1.add("C++");
   hash_set1.add("Python");
   hash_set1.add("Perl");
   hash_set1.add("HTML");
   System.out.println("Second HashSet content: "+hash_set1);
         
   hash_set.retainAll(hash_set1);
   System.out.println("HashSet content:");
   System.out.println(hash_set);
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now