Java 8 Cheat Sheet

Learn about Java syntax and methods.

We'll cover the following...

Lambda syntax

str -> System.out.println(str)
() -> System.out.println(this)
(String str) -> System.out.println(str)
(String s1, String s2) -> { return s2.length() - s1.length(); }
(s1, s2) -> s2.length() - s1.length()

Method References

Function f = Object::toString
Supplier s = thing::toString
Stream<File> pdfs = getFiles().filter(FileFilters::fileIsPdf)

Functional interfaces under java.util.function

  • `Function:

  • <T,R>`: Takes an object of type T and returns R.

  • `Supplier:

  • <T><T> ...