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!