在Java中使用3个集合的操作

时间:2021-12-02 07:27:52

I have 3 sets and i'm using sets

我有3套,我正在使用套装

 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");  

i need to calculate the union, intersection, difference and power of these sets

我需要计算这些集合的并集,交集,差异和功效

i tried this with union

我和工会一起试过这个

Set<String> uni_temp = new HashSet<String>();
uni_temp.addAll(set_1);
uni_temp.addAll(set_2);
uni_temp.addAll(set_3);

but it only makes the union of set_1 and set_2 like so

但它只是使set_1和set_2的联合像这样

[1, 2, 3, 4, 5, 6]

[1,2,3,4,5,6]

3 个解决方案

#1


2  

If you initialized set_1, set_2, and set_3 like below, your union attempt would work:

如果您初始化了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]

Edit: Your updated code would work if you added the strings to appropriate variable names instead of the unknown variable 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");

In general lets say you have two sets with variable names a and b:

一般来说,假设你有两组变量名a和b:

To get the intersection: a.retainAll(b);

得到交集:a.retainAll(b);

To get the difference: a.removeAll(b);

为了得到差异:a。删除所有(b);

#2


1  

You can try like:

您可以尝试:

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);

#3


0  

Using your example you should get the union from your code as shown in the following example

使用您的示例,您应该从代码中获取联合,如以下示例所示

    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());
    }

Output

1
2
3
4
5
6
7
8
9

Note I am using Integers instead of Strings

注意我使用的是Integers而不是Strings

#1


2  

If you initialized set_1, set_2, and set_3 like below, your union attempt would work:

如果您初始化了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]

Edit: Your updated code would work if you added the strings to appropriate variable names instead of the unknown variable 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");

In general lets say you have two sets with variable names a and b:

一般来说,假设你有两组变量名a和b:

To get the intersection: a.retainAll(b);

得到交集:a.retainAll(b);

To get the difference: a.removeAll(b);

为了得到差异:a。删除所有(b);

#2


1  

You can try like:

您可以尝试:

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);

#3


0  

Using your example you should get the union from your code as shown in the following example

使用您的示例,您应该从代码中获取联合,如以下示例所示

    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());
    }

Output

1
2
3
4
5
6
7
8
9

Note I am using Integers instead of Strings

注意我使用的是Integers而不是Strings