Sometimes, when source code gets complicated, I find it confusing to read statements like these:
Set<Integer> odds = new HashSet<>(ints); // not yet true
odds.removeAll(evens); // now it's true
I was wondering if there's a clever way to avoid a line where odds
contains also even values. Something similar to this:
(Set<Integer> odds = new HashSet<>(ints)).removeAll(evens); // doesn't compile
I could use double brace initialization,
Set<Integer> odds = new HashSet<Integer>(ints) {{ removeAll(evens); }};
but that's obviously bad for multiple reasons.
Here's another example hack that compiles, but it looks more like a joke:
Set<Integer> odds = (odds = new HashSet<>(ints)).retainAll(evens) ? odds : odds
The last one that came to my mind (while writing this) seems to be ok, although it uses two lines:
Set<Integer> odds;
(odds = new HashSet<Integer>(ints)).removeAll(evens);
Any other ideas?