Skip to content
Ondrej Kucera edited this page Dec 5, 2017 · 13 revisions
  • interface private methods - you can write helpers for more classes, but they won't be public
interface Util {
  default int getNumber() { return helper(); }
  private int helper() { return 4; }
}
  • effectively final in try-with-resources => use AutoCloseable for a class that is used in try-catch when we leave the scope on try-catch. Then the close method is automatically called. (can be dangerous!)
class Resource implements AutoCloseable {
  public void foo() { ... };
  public void close() { ... };
}

public static Sample {
  Resource resource = new Resource();
  try(resource) {
    resource.method();
}
  • No _ - underscore is not available as a name of variable

  • Iterate above IntStream + takeWhile or dropWhile

IntStream.iterate(0, i -> i + 2)
    .takeWhile(i -> i <= 5)
    .forEach(System.out::println);
Optional<Integer> first =
number.stream()
  .filter(e -> e > 50)
  .findFirst()
  .or(() -> Optional.of(77));