Loading topic
sort with an anonymous class. Java 8 lets you write that same behavior as a short lambda. A lambda can't stand on its own, though. It needs a special kind of interface to give it a type. That kind of interface is called a functional interface, and this c5 min read(a, b) -> a + b does not say what type it is. It has no class name and no interface name, and often its parameters have no types either. Java still checks it fully at compile time. This chapter shows how the compiler works out a lambda's type from the code around it.5 min readjava.util.function package covers most everyday needs. Function, Predicate, Consumer, and Supplier handle a large share of the work. Sometimes none of them fits well, and then you can write your own functional interface. This chapter shows how to do that, how the @FunctionalInterface5 min readthis and the names of your variables behave differently in each one.5 min readFunction<Integer, Integer> and Predicate<Integer>. They work. But when your lambda deals with plain numbers, Java has a better choice. This chapter shows what that choice is, why it exists, and when to use it.5 min readremoveIf and replaceAll. You also combined small functions with andThen and Predicate.and. Streams bring these ideas together. With a stream you can describe a whole chain of work on a collection, one step at a time, and give e4 min readjava.util.function, and define your own functional interfaces. Sooner or later you will try to call a method inside a lambda that throws a checked exception, and the compiler will refuse to compile your code. This chapter explains why that happens a4 min read== to each other.5 min read