Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

set union java

set1.retainAll(set2); // Intersection
Comment

set union java

set1.addAll(set2); // Union
Comment

Java Union of Sets

import java.util.HashSet;

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

        HashSet<Integer> numbers = new HashSet<>();
        numbers.add(1);
        numbers.add(3);
        System.out.println("HashSet2: " + numbers);

        // Union of two set
        numbers.addAll(evenNumbers);
        System.out.println("Union is: " + numbers);
    }
}
Comment

Java Union of Sets

import java.util.LinkedHashSet;

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

        LinkedHashSet<Integer> numbers = new LinkedHashSet<>();
        numbers.add(1);
        numbers.add(3);
        System.out.println("LinkedHashSet2: " + numbers);

        // Union of two set
        numbers.addAll(evenNumbers);
        System.out.println("Union is: " + numbers);
    }
}
Comment

Java Union 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);
        // Union of two sets
        numbers.addAll(evenNumbers);
        System.out.println("Union is: " + numbers);

    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java program scan folder find files using time 
Java :: how to send images to storage using Admin Sdk in java 
Java :: text with seek bar in android 
Java :: Java assertion with expression example 
Java :: how to get the url after loading page in webview in andorid 
Java :: java var keyword with example 
Java :: Java Static Top-level Class 
Java :: Rotate array to left k cells python 
Java :: convert int to array in java 
Java :: focus button java swing 
Java :: output 
Java :: array slicing 
Java :: infinite loop in java 
Java :: comparable interface 
Java :: save ArrayList into Room Database android studio 
Java :: intellij run single java file 
Java :: a multi-value map, Map<K, Collection<V, supporting multiple values per key 
Java :: witch dependency is needed for "ArraysUtils" for the Import? 
Java :: session-less control using java 
Java :: popcat 
Java :: arraylist character agregar elementos en java 
Java :: springfox 3.0.0 incompatibile spring boot 2.6.0 
Java :: convert object address to string in java 
Java :: all loops in java 
Java :: java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
Java :: fibonacci series 
Java :: concludes() Method 
Java :: java nested for loop 
Java :: java length of array 
Java :: java while loop 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =