Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the difference between two sets

Tags:

java

set

So if I have two sets:

Set<Integer> test1 = new HashSet<Integer>();
test1.add(1);
test1.add(2);
test1.add(3);

Set<Integer> test2 = new HashSet<Integer>();
test2.add(1);
test2.add(2);
test2.add(3);
test2.add(4);
test2.add(5);

Is there a way to compare them and only have a set of 4 and 5 returned?

like image 309
David Tunnell Avatar asked Oct 06 '22 04:10

David Tunnell


People also ask

What is the difference between a B and a B?

There is no difference. They both refer to "the open interval from a to b." The "advantage" of using ]a,b[ is that it can't be mistaken for an ordered pair.

How do you get the complement of a set and the difference of two sets?

Complement and Difference of Sets The complement of a set A is denoted by A' or Ac and it is the difference of the sets U and A, where U is the universal set. i.e., A' (or) Ac = U - A. This refers to the set of all elements that are in the universal set that are not elements of set A.

How do you find the difference between two sets?

An Example. We will look at an example of the set difference. To see how the difference of two sets forms a new set, let's consider the sets A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7, 8}. To find the difference A - B of these two sets, we begin by writing all of the elements of A, and then take away every element of A that is also an element of B.

What will be the difference between two sets a and B?

Let’s try to find out what will be the difference between two sets A and B. Then (set A – set B) will be the elements present in set A but not in B and (set B – set A) will be the elements present in set B but not in set A. Let’s look at the Venn diagram of the following difference set function.

What is the difference of two sets in set theory?

What Is the Difference of Two Sets in Set Theory? What Is the Difference of Two Sets in Set Theory? The difference of two sets, written A - B is the set of all elements of A that are not elements of B. The difference operation, along with union and intersection, is an important and fundamental set theory operation .

What is the difference between two sets in Python?

Difficulty Level : Basic Last Updated : 31 May, 2020 The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference () returns a set that is the difference between two sets.


1 Answers

Try this

test2.removeAll(test1);

Set#removeAll

Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.

like image 200
Prabhaker A Avatar answered Oct 15 '22 00:10

Prabhaker A