advertisement

Major Java 8 features

20 %
80 %
advertisement
Information about Major Java 8 features
Technology

Published on March 9, 2014

Author: sanroy

Source: slideshare.net

Description

Java 8 is coming soon. In this presentation I have outlined the major Java 8 features. You get information about interface improvements, functional interfaces, method references, lambdas, java.util.function, java.util.stream
advertisement

Major Java 8 Features Sanjoy Kumar Roy sanjoykr78@gmail.co m

Agenda Interface Improvements Functional Interfaces Lambdas Method references java.util.function java.util.stream

Interface Improvements Interfaces can now define static methods. For Example: naturalOrder method in java.util.Comparator public interface Comparator<T> { ……… public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; } ………… } 3

Interface Improvements (cont.) Interfaces can define default methods public interface A { default void foo(){ System.out.println("Calling A.foo()"); } } public class MyClass implements A { } MyClass clazz = new MyClass(); clazz.foo(); // Calling A.foo() 4

Interface Improvements (cont.) Multiple inheritance? Interface A Interface B default void foo() default void foo() Class MyClass implements A, B 5

Interface Improvements (cont.) public interface A { default void foo(){ System.out.println("Calling A.foo()"); } } CODE FAILS public interface B { default void foo(){ System.out.println("Calling A.foo()"); } } public class MyClass implements A, B { } 6

Interface Improvements (cont.) This can be fixed : Overriding in implementation class public class MyClass implements A, B { default void foo(){ System.out.println("Calling from my class."); } } OR public class MyClass implements A, B { default void foo(){ A.super.foo(); Calling the default implementation of method } foo() from interface A } 7

Interface Improvements (cont.) Another Example forEach method in java.lang.Iterable public default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } 8

Interface Improvements (cont.) Why do we need default methods in Interface? List<Person> persons = asList( new Person("Joe"), new Person("Jim"), new Person("John")); persons.forEach(p -> p.setLastName("Doe")) 9

Interface Improvements (cont.) Enhancing the behaviour of existing libraries (Collections) without breaking backwards compatibility. 10

Functional Interface An interface is a functional interface if it defines exactly one abstract method. For Example: java.lang.Runnable is a functional interface. Because it has only one abstract method. @FunctionalInterface public interface Runnable { public abstract void run(); } 11

Functional Interface (cont.) Functional interfaces are also called Single Abstract Method (SAM) interfaces. Anonymous Inner Classes can be created using these interfaces. public class MyAnonymousInnerClass { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println(“Hello!"); }}).start(); } } 12

Functional Interface (cont.) public class MyAnonymousInnerClass { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println(“Hello!"); }}).start(); Unclear } Bit excessive } Cumbersome Lambda Expressions can help us 13

How can lambda expression help us? But before that we need to see: Why are lambda expressions being added to Java? 14

Most pressing reason is: Lambda expressions make the distribute processing of collections over multiple threads easier. 15

How do we process lists and sets today ? Collection Iterator What is the issue? 16 Client Code Parallel Processing

In Java 8 - Client Code CODE AS A DATA f Collection f 17 f f f

Benefits of doing this:  Collections can now organise their iteration internally.  Responsibility for parallelisation is transferred from client code to library code. 18

But client code needs a simple way of providing a function to the collection methods. Currently the standard way of doing this is by means of an anonymous class implementation of the appropriate interface. Using a lambda, however, the same effect can be achieved much more concisely. 19

public class MyAnonymousInnerClass { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println(“Hello!"); }}).start(); } } public class MyAnonymousInnerClass { public static void main(String[] args) { new Thread(() -> System.out.println(“Hello!") ).start(); } } 20

Lambda Expression Lambda Expression (Project Lambda) is one of the most exciting feature of Java 8. Remember SAM type? In Java lambda expression is SAM type. Lambda expressions let us express instances of single-method classes more compactly. 21

So what is lambda expression? In mathematics and computing generally, a lambda expression is a function. For some or all combinations of input values 22 It specifies An output value

Lambda Syntax The basic syntax of lambda is either (parameters) -> expression or (parameters) -> { statements; } 23

Some examples (int x, int y) -> x + y // takes two integers and returns their sum (x, y) -> x – y // takes two numbers and returns their difference () -> 42 // takes no values and returns 42 (String s) -> System.out.println(s) // takes a string, prints its value to the console, and returns nothing x -> 2 * x // takes a number and returns the result of doubling it c -> { int s = c.size(); c.clear(); return s; } // takes a collection, clears it, and returns its previous size 24

public class Main { @FunctionalInterface interface Action { void run(String s); } public void action(Action action){ action.run("Hello"); } public static void main(String[] args) { new Main().action( (String s) -> System.out.print("*" + s + "*") ); } } 25 OUTPUT: *Hello*

There is a generated method lambda$0 in the decompiled class E:JAVA-8-EXAMPLESLAMBDA>javap -p Main Compiled from "Main.java" public class Main { public Main(); public void action(Main$Action); public static void main(java.lang.String[]); private static void lambda$main$0(java.lang.String); } 26

Method Reference Sometimes a lambda expression does nothing but calls an existing method. In these cases it is clearer to refer to that existing method by name. Method reference is a lambda expression for a method that already has a name. Lets see an example.... 27 © Unibet Group plc 2011

public class Person { private Integer age; public Person(Integer age) { this.setAge(age); } public Integer getAge() { return age; } private void setAge(Integer age) { this.age = age; } public static int compareByAge(Person a, Person b) { return a.getAge().compareTo(b.getAge()); } public static List<Person> createRoster() { List<Person> roster = new ArrayList<>(); roster.add(new Person(20)); roster.add(new Person(24)); roster.add(new Person(35)); return roster; } @Override public String toString() { return "Person{ age=" + age + "}"; } } 28

public class WithoutMethodReferenceTest { Without Method Reference public static void main(String[] args) { List<Person> roster = Person.createRoster(); Person[] rosterAsArray = roster.toArray(new Person[roster.size()]); class PersonAgeComparator implements Comparator<Person> { public int compare(Person a, Person b) { return a.getAge().compareTo(b.getAge()); } } // Without method reference Arrays.sort(rosterAsArray, new PersonAgeComparator()); for(int i=0; i<rosterAsArray.length; ++i){ System.out.println("" + rosterAsArray[i]); } } } 29 OUTPUT Person{age=20} Person{age=24} Person{age=35}

Ah .... PersonAgeComparator implements Comparator. Comparator is a functional interface. So lambda expression can be used instead of defining and then creating a new instance of a class that implements 30 Comparator<Person>.

With Lambda Expression public class WithLambdaExpressionTest { public static void main(String[] args) { List<Person> roster = Person.createRoster(); Person[] rosterAsArray = roster.toArray(new Person[roster.size()]); // With lambda expression Arrays.sort(rosterAsArray, (Person a, Person b) -> {return a.getAge().compareTo(b.getAge());} ); for(int i=0; i<rosterAsArray.length; ++i){ System.out.println("" + rosterAsArray[i]); } } } OUTPUT Person{age=20} Person{age=24} Person{age=35} 31

However, this method to compare the ages of two Person instances already exists as Person.compareByAge So we can invoke this method instead in the body of the lambda expression: Arrays.sort(rosterAsArray, (a, b) -> Person.compareByAge(a, b) ); But we can do it more concisely using Method Reference. 32

With Method Reference public class MethodReferenceTest { public static void main(String[] args) { List<Person> roster = Person.createRoster(); Person[] rosterAsArray = roster.toArray(new Person[roster.size()]); // With method reference Arrays.sort(rosterAsArray, Person::compareByAge ); for(int i=0; i<rosterAsArray.length; ++i){ System.out.println("" + rosterAsArray[i]); } } } OUTPUT Person{age=20} Person{age=24} Person{age=35} 33

There are four kinds of Method Reference Reference to a static method ContainingClass::staticMethodName Reference to an instance method of a particular object ContainingObject::instanceMethodName Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName Reference to a constructor ClassName::new 34

Examples Reference to a static method Person::compareByAge 35

Examples (cont.) Reference to an instance method of a particular object class ComparisonProvider { public int compareByName(Person a, Person b) { return a.getName().compareTo(b.getName()); } public int compareByAge(Person a, Person b) { return a.getBirthday().compareTo(b.getBirthday()); } } ComparisonProvider myCompProvider = new ComparisonProvider(); Arrays.sort(rosterAsArray, myCompProvider::compareByName); 36

Examples (cont.) Reference to an instance method of an arbitrary object of a particular type String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" }; Arrays.sort(stringArray, String::compareToIgnoreCase ); 37

Examples (cont.) Reference to a constructor Set<Person> rosterSet = transferElements(roster, HashSet::new ); 38

java.util.function Function<T, R> R as output Takes a T as input, returns an Predicate<T> Takes a T as input, returns a boolean as output Consumer<T> Takes a T as input, performs some action and doesn't return anything Supplier<T> Does not take anything as input, return a T And many more --39

java.util.stream Allows us to perform filter/map/reduce -like operations with the collections in Java 8. List<Book> books = … //sequential version Stream<Book> bookStream = books.stream(); //parallel version Stream<Book> parallelBookStream = books.parallelStream(); 40

Thank you.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Java 7: The Top 8 Features - JAXenter

Java 7: The Top 8 Features. ... It’s been a while since the last major Java release and expectations were naturally high for the upcoming release.
Read more

What's New in JDK 8 - Oracle | Integrated Cloud ...

Java Platform, Standard Edition 8 is a major feature release. This document summarizes features and enhancements in Java SE 8 and in JDK 8, Oracle's ...
Read more

5 Features In Java 8 That WILL Change How You Code ...

Java 8 is packed full of some really exciting features at both the JVM and language level. In this post, we'll look at 5 features that we feel are an absolute
Read more

What are the main features of Java 8 (vs Java 7)? - Quora

What are the main features of Java 8 (vs Java 7)? ... What are some major differences between Java 7 and 8? Should I program new projects in Java 7 or Java 8?
Read more

Java 1.8 - New features, code examples, performance - ttux.net

Java 1.8 (JDK8): What's new? Code examples and performance . 20 Apr 2013 at 15:54:52 - 45 comment(s) ... Here are the new Java 1.8 features I'll talk about:
Read more

Major Java 8 features - Technology - documents.mx

Java 8 is coming soon. In this presentation I have outlined the major Java 8 features. You get information about interface improvements, functional ...
Read more

Java 8 Tutorial

Java 8 is the most awaited and is a major feature release of Java programming language. This is an introductory tutorial that explains the basic-to ...
Read more

Java 8 Features - Java Tutorial Blog

“Java Platform, Standard Edition 8 (Java SE 8)” is released on 18th March 2014. Along with the Java SE 8 platform, the product that implements the platform
Read more

Java SE 7 Features and Enhancements - Oracle

Java SE 7 Features and Enhancements Java Platform, Standard Edition 7 is a major feature release. This document includes information on features and ...
Read more