Java 8 Resources - This is NOT the official website of Java.

// Lambda expressions
(parameters) -> expression                  e.g.        (int x, int y) -> x + y
(parameters) -> statement                   e.g.        () -> System.out.println("hi " + s)
(parameters) -> { statements }              e.g.        (String s) -> { int n = s.length(); return n; }
// Method references
Class::staticMethod                         e.g.        Arrays.sort(items, Util::compareItems);
instance::instanceMethod                    e.g.        items.forEach(System.out::print);
// Default methods
interface InterfaceName {                   e.g.        interface Descriptive {
    default ReturnType methodName() {                       default String describe() {
        implementation                                          return "fantastic";
    }                                                       }
}                                                       }
Let's see why Java 8 is so great!

We can start an embedded Rapidoid web server and implement a simple RESTful service with 1 line of code (and Rapidoid):
On.get("/size").json((String msg) -> msg.length());
GET /size?msg=xyz
GET /size?msg=hello
Let's create and run some Runnable! With Java 8, instead of using anonymous classes, we can use lambda expressions.
The syntax (params) -> statement is used to create an instance of Runnable by implementing its run method.
This is possible only for interfaces that have only one abstract method, and such interfaces are called functional interfaces.
Wait, aren't all interface methods abstract? Not anymore. See Defender (Default) methods.
Runnable r = () -> System.out.println("Hello!");; // prints "Hello!"
// Before Java 8, we had to write code like this:
Runnable r = new Runnable() {
    public void run() {
class Person {
    public String name;
    public int age;
List<Person> persons = new ArrayList<>(); // TODO we should add some persons to this list
// Sorting the persons by name is easy: using lambda to implement the Comparator
Collections.sort(persons, (p1, p2) ->;
// Before Java 8, we had to write code like this:
Collections.sort(persons, new Comparator<Person>() {
    public int compare(Person p1, Person p2) {
Starting from Java 8, a method defined in an interface can have a default implementation:
interface Descriptive {
    default String desc() {
        return "fantastic";
class Item implements Descriptive {
    // the method "desc" is not implemented here
Item item = new Item();
class Book implements Descriptive {
    public String desc() {
        return "Cool book!";
Book book = new Book();
book.desc();"Cool book!"
Effectively final variable is a variable that doesn't change its value (a constant).
Such variables can be referenced in lambdas without the need to explicitly mark them as "final":
// s is effectively final (not changed anywhere)
String s = "foo";
// s can be referenced in the lambda
Callable<String> callable = () -> s;;"foo"
List<String> strings = Arrays.asList("a", "", "b", "", "c");
// Count the non-empty strings
long nonEmptyCount =
        .filter(x -> !x.isEmpty())
// Count the empty strings
long emptyCount =
        .filter(String::isEmpty) // same as .filter(x -> x.isEmpty())
List<String> strings = Arrays.asList("A", "bbb", "CC", "dd");["A","bbb","CC","dd"]
// Map the strings to UPPERCASE and join them with the : separator
String joined =
List<String> words = Arrays.asList("hello", "cool", "Java8", "world!");["hello","cool","Java8","world!"]
List<Integer> lengths =
List<Integer> numbers = Arrays.asList(10, 3, 2, 5, 9, 4);[10,3,2,5,9,4]
IntSummaryStatistics stats = -> x).summaryStatistics();