-1

I have 3 sets and i'm using sets

 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

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

  • @azurefrog i updated the question – BlazePage001 Apr 11 '17 at 19:00
  • so after you do uni_temp.addAll(set_3) and you do System.out.println(uni_temp.size()) it doesn't print out 9? – Ben Arnao Apr 11 '17 at 19:00
  • 3
    Your example code is still full of statements like `set.add("7");`, when your variables are named `set_1`, `set_2`, etc. Are you sure you don't have a copy-paste error? Most likely you're adding `set_1` or `set_2` twice and `set_3` not at all. This is why it's important to create a [mcve] and then copy that actual code into your question, instead of trying to show "something like" what you're really doing. – azurefrog Apr 11 '17 at 19:14

4 Answers4

2

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

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:

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

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

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • i tried your code to make the union operation but it doesn't work the intersection and difference operations do work with 2 `sets` but i need them to work with 3 `sets` – BlazePage001 Apr 11 '17 at 19:33
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);
user2173372
  • 483
  • 7
  • 17
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

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

You can also use Sets.union() from Google Collections

Sets.union(Sets.union(a, b), c);
Danon
  • 2,771
  • 27
  • 37
  • 1
    Note that this creates an unmodifiable view of the union of a,b,c. If either of the backing sets changes after you create the view, then so does the view. This behavior may not be desirable. – Борат Сагдиев Sep 15 '22 at 14:14