查看更多教程 https://on itroad.com
获取两个字符串数组的并集
用于获取两个字符串数组之间的联合并打印输出的 Java 程序。
import java.util.Arrays;
import java.util.HashSet;
public class Main
{
public static void main(String[] args)
{
String[] firstArray = {"A", "B", "C", "D"};
String[] secondArray = {"D", "A", "E", "F"};
HashSet<String> set = new HashSet<>();
set.addAll(Arrays.asList(firstArray));
set.addAll(Arrays.asList(secondArray));
System.out.println(set);
//convert to array
String[] union = {};
union = set.toArray(union);
System.out.println(Arrays.toString(union));
}
}
程序输出。
[A, B, C, D, E, F] [A, B, C, D, E, F]
两个整数数组的并集
用于获取两个整数数组之间的联合并打印输出的 Java 程序。
import java.util.Arrays;
import java.util.HashSet;
public class Main
{
public static void main(String[] args)
{
Integer[] firstArray = {0,2,4,6,8};
Integer[] secondArray = {1,3,5,7,9};
HashSet<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(firstArray));
set.addAll(Arrays.asList(secondArray));
System.out.println(set);
//convert to array
Integer[] union = {};
union = set.toArray(union);
System.out.println(Arrays.toString(union));
}
}
程序输出。
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
日期:2020-09-17 00:09:22 来源:oir作者:oir
