这个 设置接口 存在于 JAVAutil 打包并扩展 收集接口 是一个无序的对象集合,其中无法存储重复的值。它是一个实现数学集合的接口。此接口包含从集合接口继承的方法,并添加了限制重复元素插入的功能。有两个接口扩展了set实现,即 分类集 和 NavigableSet .
方法: 以下是在Java中合并两个集合的各种方法:
- 使用双括号初始化
- 使用Set类的addAll()方法
- 使用用户定义的方法
- 在用户定义函数中使用Java 8流
- 在用户定义函数中使用Java 8流
- Stream类的()和forEach()方法的使用
- 流类的()和flatMap()方法在收集器中的使用
- 在收集器中使用Stream类的concat()方法
- 使用Apache公共集合
- 用番石榴酱。concat()
方法1: 使用双括号初始化
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
实例
JAVA
// Java Program to Demonstrate Merging of two sets in Java // Using Double brace Initialization // Importing required classes import java.io.*; import java.util.*; import java.util.stream.*; // Main class public class GFG { // Method 1 // To merge two sets // using DoubleBrace Initialisation public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Adding all elements of respective Sets // using addAll() method return new HashSet<T>() { { addAll(a); addAll(b); } }; } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); // Applying Arrays.asList() a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); // Applying Arrays.asList() b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling Method 1 to merge above Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法2: 使用Set类的addAll()方法
addAll()方法由Set接口提供。它将作为参数传递的元素添加到此集合的最后一个。
2-A。 使用用户定义的方法
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
例子:
JAVA
// Java program to demonstrate Merging of Two Sets // Using SetAll() method // Importing required classes import java.util.*; // Main class public class GFG { // Method 1 // To merge two sets // using addAll() public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Creating an empty HashSet Set<T> mergedSet = new HashSet<T>(); // Adding the two sets to be merged // into the new Set using addAll() method mergedSet.addAll(a); mergedSet.addAll(b); // Returning the merged set return mergedSet; } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First Set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second Set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge above Sets // and printing it System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2-B。 使用 Java 8流 在用户定义的函数中
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
实例
JAVA
// Java program to demonstrate Merging of Two Sets // Using Stream // Importing required classes import java.io.*; import java.util.*; import java.util.stream.*; // Main class public class GFG { // Method 1 // To merge two Sets // using addAll() public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Creating a Set with 'a' Set<T> mergedSet = a.stream().collect(Collectors.toSet()); // Adding the second set to be merged mergedSet.addAll(b); // Returning the merged Set return mergedSet; } // Method 2 // Main driver method public static void main(String[] args) { // Creating the Sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing above Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法3: 使用 Collections类的addAll()方法
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
例子:
JAVA
// Java Program to Merge Two Arrays // of Same Type into an Object Array // Importing required classes import java.io.*; import java.util.*; // Main class class GFG { // Method 1 // To merging two Sets // using addAll() public static Set<Integer> mergeSet(Set<Integer> a, Set<Integer> b) { // Creating an empty HashSet of Integer type Set<Integer> mergedSet = new HashSet<>(); // Adding the two sets to be merged // into the new Set Collections.addAll(mergedSet, a.toArray( new Integer[ 0 ])); Collections.addAll(mergedSet, b.toArray( new Integer[ 0 ])); // Returning the merged Set return mergedSet; } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the above two Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling above method 1 to merge two sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法4: 使用 of() 和 forEach() 流类的方法
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
例子:
JAVA
// Java Program to Demonstrate Merging of Two Sets // Using Stream // Importing required classes import java.io.*; import java.util.*; import java.util.stream.*; // Main class public class GFG { // Method 1 // To merge two sets // using Stream of() and forEach() methods public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Creating an empty set Set<T> mergedSet = new HashSet<T>(); // add the two sets to be merged // into the new set Stream.of(a, b).forEach(mergedSet::addAll); // returning the merged set return mergedSet; } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the above two Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法5: 使用 of() 和 flatMap() 带收集器的流类方法
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
例子:
JAVA
// Java Program to Demonstrate Merging of Two Sets // Using stream // Importing required classes import java.io.*; import java.util.*; import java.util.stream.*; // Main class public class GFG { // Method 1 // To merge two Sets // using Stream of(), flatMap() and Collector public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Adding the two Sets to be merged // into the new Set and // returning the merged set return Stream.of(a, b) .flatMap(x -> x.stream()) .collect(Collectors.toSet()); } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First Set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second Set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge above two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法6: 使用 concat() 带收集器的流类方法
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
concatenate函数用于合并到字符串,并生成包含两个字符串的单个字符串。流动concat()方法创建一个延迟连接的流,其元素是第一个流的所有元素,然后是第二个流的所有元素。
实例
JAVA
// Java program to Demonstrate Merging of two Sets // using Stream // Importing required classes import java.io.*; import java.util.*; import java.util.stream.*; // Main class public class GFG { // Method 1 // To merge two sets // using Stream concat() and Collectors public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Adding the two sets to be merged // into the new Set and // returning the merged set return Stream.concat(a.stream(), b.stream()) .collect(Collectors.toSet()); } // Method 2 // Main driver method public static void main(String[] args) { // Creating the sets to be merged // First Set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second Set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the above two Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling the method 1 to merge two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法7: 使用Apache公共集合
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
实例
JAVA
// Java Program to Demonstrate Merging of Two Sets // Using Apache Common Collection // Importing required classes import java.io.*; import java.util.*; import org.apache.commons.collections4.SetUtils; // Main class public class GFG { // Method 1 // To merge two Sets // using addAll() method public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Adding the two Sets to be merged // into the new Set and // returning the merged Set return SetUtils.union(a, b); } // Method 2 // Main driver method public static void main(String[] args) { // Creating the Sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the above two Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
方法8: 用番石榴酱。concat()
插图:
Input : a = [1, 3, 5, 7, 9] b = [0, 2, 4, 6, 8] Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
实例
JAVA
// Java Program to Demonstrate Merging of Two Sets // Using Guava Library // Importing required classes import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import java.io.*; import java.util.*; // Main class public class GFG { // Method 1 // To merge two sets // using Guava Iterables.concat() public static <T> Set<T> mergeSet(Set<T> a, Set<T> b) { // Adding the two sets to be merged // into the new set and // returning the merged set return Sets.newHashSet(Iterables.concat(a, b)); } // Method 2 // Main driver method public static void main(String[] args) { // Creating the Sets to be merged // First set Set<Integer> a = new HashSet<Integer>(); a.addAll( Arrays.asList( new Integer[] { 1 , 3 , 5 , 7 , 9 })); // Second set Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( new Integer[] { 0 , 2 , 4 , 6 , 8 })); // Printing the above two Sets System.out.println( "Set a: " + a); System.out.println( "Set b: " + b); // Calling method 1 to merge two Sets System.out.println( "Merged Set: " + mergeSet(a, b)); } } |
Set a: [1, 3, 5, 7, 9] Set b: [0, 2, 4, 6, 8] Merged Set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
注: 在上述所有方法的合并过程中,集合中出现的任何重复元素都将被丢弃。