在Java中合并两个集合
set 接口存在于Java.util 包中,并扩展了 Collection 接口,它是一个无序的对象集合,其中不能存储重复值。它是一个实现数学集的接口。该接口包含从 Collection 接口继承的方法,并添加了限制重复元素插入的功能。有两个扩展集合实现的接口,即 SortedSet 和 NavigableSet。
方法:以下是在Java中合并两个集合的各种方法:
- 使用双括号初始化
- 使用 Set 类的 addAll() 方法
- 使用用户定义的方法
- 在用户定义函数中使用Java 8 流
- 在用户定义函数中使用Java 8 流
- Stream类的of()和forEach()方法的使用
- 使用 Collector 的 Stream 类的 of() 和 flatMap() 方法
- 使用带有收集器的 Stream 类的 concat() 方法
- 使用 Apache 公共集合
- 使用 Guava Iterables.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 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 Set mergeSet(Set a, Set b)
{
// Adding all elements of respective Sets
// using addAll() method
return new HashSet() {
{
addAll(a);
addAll(b);
}
};
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating the sets to be merged
// First set
Set a = new HashSet();
// Applying Arrays.asList()
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
// 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));
}
}
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 Set mergeSet(Set a, Set b)
{
// Creating an empty HashSet
Set mergedSet = new HashSet();
// 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set b)
{
// Creating a Set with 'a'
Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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));
}
}
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 mergeSet(Set a,
Set b)
{
// Creating an empty HashSet of Integer type
Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set b)
{
// Creating an empty set
Set mergedSet = new HashSet();
// 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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));
}
}
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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]
方法二:使用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 Set mergeSet(Set a, Set b)
{
// Creating an empty HashSet
Set mergedSet = new HashSet();
// 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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 Set mergeSet(Set a, Set b)
{
// Creating a Set with 'a'
Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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]
方法三:使用 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 mergeSet(Set a,
Set b)
{
// Creating an empty HashSet of Integer type
Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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]
方法四:使用Stream类的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 Set mergeSet(Set a, Set b)
{
// Creating an empty set
Set mergedSet = new HashSet();
// 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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]
方法五:使用Stream类的of()和flatMap()方法和Collector
插图:
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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]
方法六:使用Stream类的concat()方法和Collector
插图:
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函数用于合并到字符串并生成一个包含两个字符串的字符串。 Stream.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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second Set
Set b = new HashSet();
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 Common Collections
插图:
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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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:使用 Guava Iterables.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 Set mergeSet(Set a, Set 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 a = new HashSet();
a.addAll(
Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
// Second set
Set b = new HashSet();
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]
Note: Any duplicate element presented in the sets will be discarded during the merge in all the above methods.