You are viewing a single comment's thread from:

RE: Share your favorite code snippet

in #programming9 years ago

Alternate way of swapping two vars without using a third:

a ^= b;
b ^= a;
a ^= b;

I'm also a fan of Java 8 streams:

public class MyObj {
    int someValue;
    // ...
}

List<MyObj> someList; // already defined

someList.stream()
  .map(myObj -> myObj.someValue)
  .filter(value -> value > 0)
  .findFirst().orElse(null);
Sort:  

Cool XOR swap. Didn't know this trick! Really neat.