Skip to content

Instantly share code, notes, and snippets.

@felipebelluco
Created August 9, 2017 00:51
Show Gist options
  • Save felipebelluco/3eee9070eba7df63a3552f45a171145e to your computer and use it in GitHub Desktop.
Save felipebelluco/3eee9070eba7df63a3552f45a171145e to your computer and use it in GitHub Desktop.
import java.util.*;
import static br.com.felipebelluco.javaplayground.PredicateUtil.negate;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("foo", "", "bar");
// Instead of this...
list.stream().filter(name -> !name.isEmpty()).forEach(System.out::println);
// Use this
list.stream().filter(negate(String::isEmpty)).forEach(System.out::println);
}
}
import java.util.function.Predicate;
public class PredicateUtil {
static <T> Predicate<T> negate(Predicate<T> predicate) {
return predicate.negate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment