Java 合并两个集合
集合接口存在于java.util包中,它扩展了Collection接口,是一个无序的对象集合,其中不能存储重复的值。它是一个实现了数学集合的接口。这个接口包含了从集合接口继承的方法,并增加了一个限制插入重复元素的功能。有两个接口扩展了集合的实现,即SortedSet和NavigableSet。
方法: 以下是Java中合并两个集合的各种方法。
- 使用双括号初始化
- 使用Set类的addAll()方法
- 使用用户定义的方法
- 在用户定义的函数中使用Java 8流
- 在用户定义的函数中使用Java 8 stream
- 使用流类的of()和forEach()方法
- 在采集器中使用流类的of()和flatMap()方法
- 用采集器使用流类的concat()方法
- 使用Apache普通集合
- 使用Guava Iterables.concat()
方法1: 使用Double brace初始化
示例
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 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 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 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: 使用集合类的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 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 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 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函数用于合并字符串,并使其成为一个包含两个字符串的单一字符串。Stream.concat()方法创建了一个懒惰地连接的流,其元素是第一个流的所有元素,然后是第二个流的所有元素。
例子
// 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 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: 使用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 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]
注意: 在上述所有方法的合并过程中,集合中出现的任何重复的元素都将被丢弃。