Java中的3集操作
我有3套,我正在使用Java中的3集操作,java,set,operations,Java,Set,Operations,我有3套,我正在使用set HashSet<String> set_1 =new HashSet<String>(); set_1.add("1"); set_1.add("2"); set_1.add("3"); HashSet<String> set_2 =new HashSet<String>(); set_2.add("4"); set_2.add("5"); set_2.add("6");
set
HashSet<String> set_1 =new HashSet<String>();
set_1.add("1");
set_1.add("2");
set_1.add("3");
HashSet<String> set_2 =new HashSet<String>();
set_2.add("4");
set_2.add("5");
set_2.add("6");
HashSet<String> set_3 =new HashSet<String>();
set_3.add("7");
set_3.add("8");
set_3.add("9");
HashSet_1=newhashset();
第1组。添加(“1”);
第1组添加(“2”);
第1组添加(“3”);
HashSet_2=新HashSet();
第2组添加(“4”);
第2组添加(“5”);
第2组添加(“6”);
HashSet_3=新HashSet();
第3组添加(“7”);
第3组添加(“8”);
第3组添加(“9”);
我需要计算这些集合的并、交、差和幂
我和union试过了
Set<String> uni_temp = new HashSet<String>();
uni_temp.addAll(set_1);
uni_temp.addAll(set_2);
uni_temp.addAll(set_3);
Set uni_temp=new HashSet();
统一温度添加(设置1);
统一温度添加(设置2);
统一温度添加(设置3);
但它只会使set_1
和set_2
的结合像这样
[1,2,3,4,5,6]
使用您的示例,您应该从代码中获取联合,如以下示例所示
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
Set<Integer> set3 = new HashSet<>();
for(int i = 1; i < 4; i++)
{
set1.add(i);
set2.add(i + 3);
set3.add(i + 6);
}
Set<Integer> uni_temp = new HashSet<Integer>();
uni_temp.addAll(set1);
uni_temp.addAll(set2);
uni_temp.addAll(set3);
java.util.Iterator<Integer> iterator = uni_temp.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
注意:我使用的是整数而不是字符串如果像下面这样初始化
set_1
、set_2
和set_3
,则您的并集尝试将起作用:
Set<String> set_1 = new HashSet<String>(Arrays.asList("1", "2", "3"));
Set<String> set_2 = new HashSet<String>(Arrays.asList("4", "5", "6"));
Set<String> set_3 = new HashSet<String>(Arrays.asList("7", "8", "9"));
Set<String> uni_temp = new HashSet<String>();
uni_temp.addAll(set_1);
uni_temp.addAll(set_2);
uni_temp.addAll(set_3);
System.out.println(Arrays.toString(uni_temp.toArray())); //[1, 2, 3, 4, 5, 7, 8, 9]
一般来说,假设您有两个变量名为
a
和b
的集合:
要获得交叉口:a.retainal(b)代码>
要获得差异:a.removeAll(b)代码>您可以尝试以下方法:
HashSet<String> union = new HashSet<String>(set_1);
union.addAll(set_2);
union.addAll(set_3);
System.out.println(union);
HashSet<String> intersection = new HashSet<String>(set_1);
intersection.retainAll(set_2);
intersection.retainAll(set_3);
System.out.println(intersection);
HashSet union=新的HashSet(set_1);
union.addAll(集合2);
union.addAll(集合3);
系统输出打印LN(联合);
HashSet交集=新HashSet(set_1);
交叉口。保留(集合2);
交叉口。保留(第3组);
系统输出打印LN(交叉点);
您还可以使用Google Collections中的set.union()
Sets.union(Sets.union(a, b), c);
@azurefrog我更新了问题,所以在您执行uni_temp.addAll(set_3)和System.out.println(uni_temp.size())之后,它不会打印出9?您的示例代码仍然充满了类似于set.add(“7”)的语句代码>,当变量名为set_1
、set_2
等时。是否确实没有复制粘贴错误?很可能您添加了两次set_1
或set_2
,而set_3
根本没有添加。这就是为什么创建一个脚本,然后将实际代码复制到问题中,而不是试图显示“类似”的内容是很重要的你真正在做什么。我试着用你的代码来做并集运算,但它不起作用。交叉和差分运算可以处理2个集合
,但我需要它们处理3个集合
HashSet<String> union = new HashSet<String>(set_1);
union.addAll(set_2);
union.addAll(set_3);
System.out.println(union);
HashSet<String> intersection = new HashSet<String>(set_1);
intersection.retainAll(set_2);
intersection.retainAll(set_3);
System.out.println(intersection);
Sets.union(Sets.union(a, b), c);