2. What has been happening in
Computer
❖ Moor’s Law no longer applicable
❖ Computer speed is not increasing unlimited
❖ MultiCore Computer
❖ Pre-Java8 does not take advantage of multicore
computer Power
❖ Threading is hard, multicore make it harder
3. Computer Languages
❖ Groovy, Scala, Clojure, Kotlin, JavaScript/CoffeeScript,
Go, Swift, Lua, ErLang, Haskell, Ruby,C#,F#,…
❖ Closure
❖ Lambda Expression
❖ Pre-Java8 Did not have this, so what?
4. What is Closure
“In computer science, a closure is a first-class function with
free variables that are bound in the lexical environment.”
6. What is Lambda Expression
❖ A concise representation of an anonymous
function that can be passed around:
❖ doesn't have a name, but has
❖ a list of parameters
❖ a body
❖ a return type
❖ possibly a list of exceptions
7. Example Lambda Expression
//Anonymous version
Runnable r1 = new Runnable(){
@Override
public void run(){
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
// Run em!
r1.run();
r2.run();
Compare the one-liner lambda with the 4+ lines legacy code!
8. What’s new in Java8
❖ Lambda expression
❖ Default methods
❖ Stream
❖ optionals
❖ Date/Time API
9. What Lambda Expression
brings?
❖ Pass code concisely-functions are passed as values
❖ Collection:
❖ List<Person> pl ;//Given a list of Persons
❖ looping: pl.forEach( p -> p.printName() );
❖ filtering and chaining:
❖ pl.stream().filter(p->p.getAge()>30).sort(comparing((a)-
>a.getAge())).forEach(Person::printName);
❖ This one line replaces 20 lines of traditional code and does more
(later on stream)
12. Default Methods
❖ Interface can contain method signatures for which an
implementation class doesn’t have to provide
implementation
❖ interface can now provide default method
implementation
13. Streams
❖ Manipulate collection of data in a declarative way.
❖ Streams can be processed in parallel transparently
without having to write any multithreaded code!
20. Optionals
❖ how to model “absence of a value”
❖ Scala introduced Option[T]
❖ Java 8 call it Optional[T]
❖ Optional<String> version =
optcomputer.map(Computer::getSoundcard).map(Sound
Card::getUSB).map(USB::getVersion)
21. Beyond Java 8
❖ Functional programming: Focus on What
❖ Scala example
❖ flatten(List(List(1, 1), 2, List(3, List(5, 8))))
❖ result: List(1, 1, 2, 3, 5, 8)
❖ Imperative programming: Focus on How
❖ Many lines needed to do the above 1-line job
❖ Functional programming is itself a great topic!