There are two Tree sets in my app.

```
set1 ={501,502,503,504}
set2 ={502,503,504,505}
```

I want to prform set1 - set2

so that my output would be a set

```
set={501,505}
```

You're after the symmetric difference. This is discussed in the Java tutorial.

```
Set<Type> symmetricDiff = new HashSet<Type>(set1);
symmetricDiff.addAll(set2);
// symmetricDiff now contains the union
Set<Type> tmp = new HashSet<Type>(set1);
tmp.retainAll(set2);
// tmp now contains the intersection
symmetricDiff.removeAll(tmp);
// union minus intersection equals symmetric-difference
```

use retain all,remove all then addAll to do a union of existing set.

- intersectionSet.retainAll(set2) // intersectionSet is a copy of set1
- set1.addAll(set2); // do a union of set1 and set2
- then remove the duplicates set1.removeAll(intersectionSet);

```
Set<String> s1 = new HashSet<String>();
Set<String> s2 = new HashSet<String>();
s1.add("a");
s1.add("b");
s2.add("b");
s2.add("c");
Set<String> s3 = new HashSet<String>(s1);
s1.removeAll(s2);
s2.removeAll(s3);
s1.addAll(s2);
System.out.println(s1);
```

output of s1 : [a,c]

You could use `CollectionUtils#disjunction`

EDIT:

Alternatively with less pre-Java-5-ness, use Guava Sets#symmetricDifference

For those looking for set subtraction (not symmetric difference/disjunction), you can use `CollectionUtils#subtract`

.

Similar Questions

I have two dataframes. First there is DF1: ID Other value 1 a 2 b 3 c and then there is DF2, which is a subset of DF1: ID Other value 1 a I want to create a third dataframe that would be the equival

I have 2 sets of integers, A and B, not necessarily of the same size. For my needs, I take the distance between each 2 elements a and b (integers) to be just abs(a-b). I am defining the distance betwe

I'm trying to convert some opencv code from C++ to Java, but I'm stuck at this code: Mat matXyz; // some Mat Mat result; // some other Mat // ... set above mats to some values ... result = Mat::ones(m

Can anybody help me to alter this JQuery code so that it will do a subtraction calculation rather than addition? In this current code the first table is just a normal table holding the data, the secon

I have two sets, each set is a listing of a pair of numbers Set1 =[(x1, y1), (x2, y2), ..., (xN, yN)] Set2 =[(a1, b1), (a2, b2), ..., (aN, bN)] If plotted on an XY plane, Set1 and Set2 have the same

I want to use Java's Area class (java.awt.geom.Area) to preform subtraction and intersection operations on various polygons. In many of these cases the subtraction operation may split the source Area

What is the fastest way to find out whether two sets are different? Using std::set_difference gets me all the differences, but I only need to know about the first diff. Currently, I am doing the follo

Possible Duplicate: How do you subtract Dates in Java? I am creating java desktop application. In that application user give some date inputs. so i use 3party library jCalendar.jar. for validation

I came across this in a computer architecture textbook: Subtracting a strictly negative integer from another strictly negative integer (in two's complement) will never overflow. The textbook doesn't

I have two sets of 3D points (original and reconstructed) and correspondence information about pairs - which point from one set represents the second one. I need to find 3D translation and scaling fac

I have two properties in Ant that both contain integers. I want to check if one is greater than the other. How can I accomplish that? Is there a way to use subtraction in ant? Then I could just subtra

This is the practice example : Write a function (list1, list2) that takes in two lists as arguments and return a list that is the result of removing elements from list1 that can be found in list2. W

I have a script that is supposed collect two sets of high and low numbers and incorporate them into a single string: a_low and a_high b_low and b_high I had found a script and tweaked it to generate o

I have two lists of numbers, say [1, 2, 3, 4, 5] and [7, 8, 9, 10, 11], and I would like to form a new list which consists of the products of each member in the first list with each member in the seco

How do you go about overloading the addition, subtraction, and multiplication operator so we can add, subtract, and multiply two vectors of different or identical sizes? For example, if the vectors ar

E.g. the inputs are two sets A and B. set A is stored in file a.txt as below: apple orange grape ... set B is stored in file b.txt as below: tomato potato cucumber ... The output is c.txt like: appl

Here are my two data frames, both have the same amount of rows which are 70492, I would like to merge them together, This will not merge my data frames together final <- merge(DF1,DF2) The error

I have two tables in my Report. Both use two separate Data Sets, which get data from the same Stored Procedure. Now, I want the data sets to get different data based on different parameter values. Is

I have for example two namespaces with three classes in each namespace.These two sets of three classes are--for my purposes--identical in names and properties, but each set exists in its own namespace

I need to plot data from a .csv file and from a white space separated file. Both sets of data need to appear on the same plot. data1.dat #t y 1 1 2 1 3 1 and data2.csv #t,y 1,2 2,2 3,2 normally I

I have been struggling to find the best way to do this subtraction within groups. I have data frame containing a long list of samples (Sample), each one treated with different conditions (condition),

Say I have two sets of data, each entry consisting of a weight. Each set is ordered by weight ascending. I'm going to list a couple example sets: Set 0: {4, 8, 19, 25, 25, 26} Set 1: {1, 2, 3, 8, 20,

Here is the interesting but complicated problem: Suppose we have two sets of points. One set A includes points in some space grid, like regular 1D or 3D grid. The other set B includes points that are

I have two lists ( not java lists, you can say two columns) For example **List 1** **Lists 2** milan hafil dingo iga iga dingo elpha binga hafil mike meat dingo milan elpha meat iga neeta.peeta I'd l

I have two sets. (from Guava HashMultimap.values()). I need to quickly find, if the intersection of the two set is a non-empty set. I do not need to know about the common elements, just if there is a

Given 2 collections of elements, S1 and S2, what is the most efficient way to compute 3 sets (A, B, C), where A is the unique elements of S1 and B is unique elements of S2 and C is common items of S1

I have two data sets A and B and i wanna to find the correlation and plot the contour map. A is just a simple vector with 230 stream flow data. B is a complicated sea surface temperature(SST) data und

Once an object file of a program is created does it matter if I run only one or multiple instances of it on different input data sets? Is it possible that outputs corresponding to these runs may be co

import time print time.time() - time.time() the result unit is millisecond or second? what I want do is judge if the two operation time span is larger than 10 minutes

What is the most efficient way to find the size of the intersection of two non-sparse Sets in Java? This is an operation I will be calling on large sets a very large number of times, so optimisation i

I have a problem with a simple subtraction but I don't understand what's wrong. My code : start= date +%s%N | cut -b1-13 #Treatment... end= date +%s%N | cut -b1-13 delta=`expr $end - $start` echo de

This is my homework question i have tried to solve it just need someone to look and tell me if i am doing it right or worng.. The dynamic-set operation UNION takes two disjoint sets S1 and S2 as input

I need to find matching between two independent sets of features extracted from two images of the same scene captured by two different cameras. I'm using the HumanEvaI data set, so I have the calibrat

I'm wondering if there is a quick/clean way to get the difference between two sets ? I have: Set<String> s1 = new HashSet<String>(); s1.add(a); s1.add(b); s1.add(c); Set<String>

Is there an easy way to get the relative complement of two sets? Perhaps using LINQ? I have to find the relative compliment of a set A relative to B. Both A and B are of type HashSet<T> but I th

I want to subtract two dates (one constant and one current) in Java but I've got strange problems with it. Here is the code : DateFormat df = new SimpleDateFormat(HH:MM); Date FirstLessonInterval=d

Lets say, I have employees, and I know what fruits they like. fruits(name, fruit_name) My question is: list all the employees which at least like the same fruits as Donald. So how do I compare two se

I have two sets of points, A and B, and I'm trying to find the closest pair of points where one point is taken from each set. That is, if you were to use the points two draw to lines, I want the two p

Is there a way to copy a treeset? That is, is it possible to go Set <Item> itemList Set <Item> tempList tempList = itemList or do you have to physically iterate through the sets and copy

I am creating a java application which consists of two frames(JFrame1 and JFrame2) JFrame1 has a grid 6x6 button; and JFrame2 has 6 radio buttons representing colours. How can I link the two frames so

I am trying to subtract two values in my array the largest and the smallest. I have sorted them then tried to subtract them but the output is different. I am also trying to use a loop but I keep getti

I'm working on a simple subtraction problem, but unfortunately it keeps returning NaN Here is the function function subtraction(a, b) { var regexp = /[$][,]/g; a = a.replace(regexp, ); b = b.replac

I was making a project in Greenfoot and I made an imaginary number class. In my project I found a need to add (or subtract, or whatever) two imaginary objects together, is there a way to add two objec

How to calculate subtraction (set operation) of ordered lists in C#? I am interesed in concise solution. E.g. after execution of code: List<int> a = new int[] { 1, 2, 5, 6, 7}.ToList(); List<

I have to make almost what is said in Efficient Background subtraction with Opencv (Background subtraction with the foreground with colour except with a camera and not a video file). The problem is th

add rd, rs, rt addu rd, rs, rt sub rd, rs, rt subu rd, rs, rt In MIPS, what's the difference between signed addition, unsigned addition, signed subtraction and unsigned subtraction? If their results

I know you can achieve o(n) in a sorted sets but I have looked around stackoverflow and couldn't find any answer that would be in o(n) efficiency, only o(n^2). Is there a way to achieve o(n) or is it

In MATLAB I have two vectors which are both 1 x 310 of type doubles. So I have a line shown below. ret & bret are my vectors. This result produces another vector act_r of 1 X 310 of type double -

HI, I have two recordsets rs1 and rs2 in vb6. i want to compare each record in rs1 with each record in rs2. If rtn(column) in rs1 is same as in rtn in rs2.then set date = now and claim = c. This is wh

I have two java applications. They are very simple: they insert 500,000 rows of fake data into a MongoDB database and SQL database respectively. I time each operation. How can I launch these two java