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 :: reverse arraylist java recursion 
Java :: java empty arraylist 
Java :: boolean operators in java 
Java :: types of methods in java 
Java :: how to get app categories android packagemanager 
Java :: what is static method in oop 
Java :: how to create search function in android studio 
Java :: java regex for ip address 
Java :: spring boot api key authentication example 
Java :: java exponencial 
Java :: 9999999999999 
Java :: jsoup remove element 
Java :: spring security antmatchers id 
Java :: android send fragment context 
Java :: t implements comparable 
Java :: if ternaire java 
Java :: log messages not printing in springboot using org.slf4j.Logger; 
Java :: one line hashmap java 
Java :: Java 2-dimensional Array 
Java :: How to change integers into float in java 
Java :: hash map java 
Java :: java scanner next() 
Java :: remove duplicates from arraylist in android 
Java :: syntax of the switch statement in Java 
Java :: java data structure 
Java :: binary tree traversal 
Java :: java servlet life cycle 
Java :: math.pow(x2-x1,2) 
Java :: logcat android 
Java :: instantiation in java 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =