Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

set intersection java

set1.retainAll(set2);
Comment

Java Intersection of Sets

import java.util.LinkedHashSet;

class Main {
    public static void main(String[] args) {
        LinkedHashSet<Integer> primeNumbers = new LinkedHashSet<>();
        primeNumbers.add(2);
        primeNumbers.add(3);
        System.out.println("LinkedHashSet1: " + primeNumbers);

        LinkedHashSet<Integer> evenNumbers = new LinkedHashSet<>();
        evenNumbers.add(2);
        evenNumbers.add(4);
        System.out.println("LinkedHashSet2: " + evenNumbers);

        // Intersection of two sets
        evenNumbers.retainAll(primeNumbers);
        System.out.println("Intersection is: " + evenNumbers);
    }
}
Comment

Java Intersection of Sets

import java.util.TreeSet;;
class Main {
    public static void main(String[] args) {
        TreeSet<Integer> evenNumbers = new TreeSet<>();
        evenNumbers.add(2);
        evenNumbers.add(4);
        System.out.println("TreeSet1: " + evenNumbers);

        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("TreeSet2: " + numbers);

        // Intersection of two sets
        numbers.retainAll(evenNumbers);
        System.out.println("Intersection is: " + numbers);
    }
}
Comment

Java Intersection of Sets

import java.util.HashSet;

class Main {
    public static void main(String[] args) {
        HashSet<Integer> primeNumbers = new HashSet<>();
        primeNumbers.add(2);
        primeNumbers.add(3);
        System.out.println("HashSet1: " + primeNumbers);

        HashSet<Integer> evenNumbers = new HashSet<>();
        evenNumbers.add(2);
        evenNumbers.add(4);
        System.out.println("HashSet2: " + evenNumbers);

        // Intersection of two sets
        evenNumbers.retainAll(primeNumbers);
        System.out.println("Intersection is: " + evenNumbers);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: execute code after delay java 
Java :: javafx location is required 
Java :: cosinus-1 java 
Java :: java linkedlist print 
Java :: arrays sort 2d array java 
Java :: 64 encode java 
Java :: android date time 
Java :: Unsupported Modules Detected 
Java :: java encrypt string 
Java :: Diamond pattern program in java 
Java :: java delete directory with files 
Java :: write a name and convert it to an array 
Java :: parse string to int java 
Java :: java const 
Java :: count vowels in java 
Java :: java array to string 
Java :: view get text android 
Java :: comparable on a generic class java 
Java :: java home mac 
Java :: error cannot find symbol intent 
Java :: hello world in different programming languages 
Java :: open camera or gallery on button click android 
Java :: how to check if a string is numeric 
Java :: iterable interface in java 
Java :: how to find a word in string in java 
Java :: volley dependency 
Java :: java while loop example 
Java :: javafx change textfield background color 
Java :: discord jda 
Java :: java subscript numbers 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =