Write a Java Program to Calculate the intersection of two sets

Java Program to Calculate the intersection of two sets

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Calculate the intersection of two sets
Java Program to Calculate the intersection of two sets

Write a Java Program to Calculate the intersection of two sets

PROGRAM: Java Program to Calculate the intersection of two sets

/*Java Program to Calculate the intersection of two sets*/

 1: Calculate the intersection of two sets

import java.util.HashSet;
import java.util.Set;

class Main {
public static void main(String[] args) {

// create first set
Set<Integer> primeNumbers = new HashSet<>();
primeNumbers.add(2);
primeNumbers.add(3);
System.out.println(“Prime Numbers: ” + primeNumbers);

// create second set
Set<Integer> evenNumbers = new HashSet<>();
evenNumbers.add(2);
evenNumbers.add(4);
System.out.println(“Even Numbers: ” + evenNumbers);

// Intersection of two sets
evenNumbers.retainAll(primeNumbers);
System.out.println(“Intersection: ” + evenNumbers);
}
}

2: Get union of two sets using Guava Library

import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.Sets;

class Main {
public static void main(String[] args) {

// create the first set
Set<String> backend = new HashSet<>();
backend.add(“Java”);
backend.add(“JavaScript”);
System.out.println(“Backend Languages: ” + backend);

// create second set
Set<String> frontend = new HashSet<>();
frontend.add(“JavaScript”);
frontend.add(“CSS”);
System.out.println(“Frontend Languages: ” + frontend);

Set<String> intersect = Sets.intersection(backend, frontend);
System.out.println(“Common Languages: ” + intersect);
}
}



Leave a Reply

Your email address will not be published. Required fields are marked *