question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sets.difference should accept general Collection as second argument

See original GitHub issue

Current signature of Sets.difference method is

SetView<E> difference(final Set<E> set1, final Set<?> set2)

however, for all usages of set2 argument everything would work as well if argument was of type Collection<?>, without impact on performance for current usages. Why not to make signature less strict?

In my use case, I do following manipulation:

BiMap<Foo,Foo> specialFoos = ...
Set<Foo> nonspecialLeftFoos  = Sets.difference(leftFoos ,specialFoos.keySet());
Set<Foo> nonspecialRightFoos = Sets.difference(rightFoos,specialFoos.values());

where this issue is the only reason why specialFoos is a BiMap. Thanks for response.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jbduncancommented, Feb 13, 2017

If you really want that behavior you can also always get it with Sets.filter(set, not(in(Collection))).

Or, if you’re on Java 8 and a fan of lambdas: Sets.filter(set, e -> !collection.contains(e)).

1reaction
lowassercommented, Feb 13, 2017

If you really want that behavior you can also always get it with Sets.filter(set, not(in(Collection))).

On Mon, Feb 13, 2017, 9:03 AM Michał Sobkiewicz notifications@github.com wrote:

(…) for all usages of set2 argument everything would work as well if argument was of type Collection<?>, without impact on performance for current usages.

Collection.contains(), which Sets.difference() uses a lot, is O(1) for hash based sets and O(log(n)) for tree based sets. You can’t guarantee anything below O(n) in general (List, Queue and so on). Iterating over Sets.difference() would be O(n^2) in that case - that’s not something you want - it’s about O(n) now.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/guava/issues/2738#issuecomment-279454201, or mute the thread https://github.com/notifications/unsubscribe-auth/AAhPObEnpQzq_tLYJbM7coIWoy1r034lks5rcIzbgaJpZM4L_Zdk .

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Set Interface - Java™ Tutorials
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited...
Read more >
Sets | Collections (Scala 2.8 - 2.12)
The operations on sets are summarized in the following table for general sets ... except that ++ takes a Traversable argument whereas union...
Read more >
Sets in Python - Real Python
Set elements are unique. Duplicate elements are not allowed. A set itself may be modified, but the elements contained in the set must...
Read more >
Sets in Julia - GeeksforGeeks
Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered ...
Read more >
SetUtils (Apache Commons Collections 4.4 API)
Returns a unmodifiable view containing the difference of the given Set s, ... the second set, must not be null; Returns: a view...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found