Getting the Difference Between Two Sets

Getting the difference between two sets

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.

How to: Find the Set Difference Between Two Collections in Java

I have used Guava Sets.difference.

The parameters are sets and not general collections, but a handy way to create sets from any collection (with unique items) is Guava ImmutableSet.copyOf(Iterable).

how to find difference between 2 sets without making change of any one?

You can use this :

Set<String> s3 = new HashSet<String>();
for(String temp : s1){
if(!s2.contains(temp)){
s3.add(temp);
}
}
for (String temp : s2) {
if (!s1.contains(temp)) {
s3.add(temp);
}
}

How can I get a list containing the differences between two sets?

Using streams it can be done with Collectors.groupingBy and Collectors.counting() to determine the count of occurrences and then filter any string that appears more than once:

List<String> differences = Stream.concat(set1.stream(), set2.stream())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) // Map<String, Long> -> key -> element, value -> count of occurrences
.entrySet().stream()
.filter(e -> e.getValue() == 1) // filter not unique elements from 2 sets
.map(Map.Entry::getKey)
.collect(Collectors.toList());

What is the best way get the symmetric difference between two sets in java?

You can use some functions from the Google Guava library (which is really great, I strongly recommend it!):

Sets.difference(s1, s2);
Sets.symmetricDifference(s1, s2);

Javadocs for difference() and symmetricDifference()

symmetricDifference() does exactly what you are asking for, but difference() is also often helpful.

Both methods return a live view, but you can for example call .immutableCopy() on the resulting set to get a non-changing set. If you don't want a view, but need a set instance you can modify, call .copyInto(s3). See SetView for these methods.

How to work with Set Differences in Java using objects?

You can achieve this with the help of overloaded subtract with predicate.

  1. Override hashcode and equals in POJO class, (and toString for clean output)

    @Override
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    POJO pojo = (POJO) o;
    return Objects.equals(name, pojo.name);
    }

    @Override
    public int hashCode() {
    return name.hashCode();
    }
  2. Use Predicate with set as

    Collection<POJO> subList = CollectionUtils.subtract(p1, p2, new Predicate<POJO>() {
    @Override
    public boolean evaluate(POJO object) {
    return p1.contains(object); // might need to make p1 final
    }
    });

Result:

subList: [ Bagel ]

Note: Apache common does not support anything like Java 8 BiPredicate

python set to take the difference of name values between two files

First, let's address your loading code. You don't need to construct a list for each file: you can add elements directly to a set. Sets accept any reasonable iterable as input to their constructor. Files are iterable over their lines, so you can truncate your initialization to four lines:

with open('file_noi1', 'r') as f:
dataset1 = set(f)
with open('file_noi2', 'r') as f:
dataset2 = set(f)

From your example, you are looking either for difference, which is implemented through the - operator, or symmetric_difference, implemented through the ^ operator. Since dataset2 is strictly a subset of dataset1, both of the following will give the desired result:

dataset1 - dataset2

AND

dataset1 ^ dataset2

The difference (no pun intended) is that the latter operation will contain elements from either set which aren't in the other, and therefore commutes.

Keep in mind that sets aren't ordered, so enumerating over a set may not be all that useful unless you sort it first:

for i, e in enumerate(sorted(dataset1 ^ dataset2)):
...

Get difference between two lists with Unique Entries

To get elements which are in temp1 but not in temp2 (assuming uniqueness of the elements in each list):

In [5]: list(set(temp1) - set(temp2))
Out[5]: ['Four', 'Three']

Beware that it is asymmetric :

In [5]: set([1, 2]) - set([2, 3])
Out[5]: set([1])

where you might expect/want it to equal set([1, 3]). If you do want set([1, 3]) as your answer, you can use set([1, 2]).symmetric_difference(set([2, 3])).



Related Topics



Leave a reply



Submit