java 8 intro - core features

17
Java 8 intro : Core features Spring 2016 Arkadii Tetelman

Upload: globallogic-ukraine

Post on 08-Apr-2017

1.012 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Java 8 Intro - Core Features

Java 8 intro : Core features

Spring 2016 Arkadii Tetelman

Page 2: Java 8 Intro - Core Features

2

Java 8 New Features (Core and Collections)

• Interface’s Default (default methods)

• Functional Interfaces

• Lambdas

• Method References

• Streams (Stream API)

• Optional

Page 3: Java 8 Intro - Core Features

3

Interface’s Default (default methods)

Java 8 extends interface declarations with two new items:

• default (default method implementation) • static methods• functional interface

Page 4: Java 8 Intro - Core Features

4

Default Method

Default methods adds default method implementation that would be used in the case when the implementer class won’t implement this method .

default is a new keyword in Java8 syntax

When you extend an interface that contains a default method, you can do the following:

• Not mention the default method at all, which lets your extended interface inherit the default method.

• Re-declare the default method, which makes it abstract.• Redefine the default method, which overrides it.

Page 5: Java 8 Intro - Core Features

5

Default Method code example

public interface DefaultExample { default String defaultMethod() { return "Default implementation"; }}

private class DefaultableImpl implements DefaultExample {}

protected abstract class DefaultableAbstractImpl implements DefaultExample { public abstract String defaultMethod();}

class OverrideImpl implements DefaultExample { @Override public String defaultMethod() { return "Overridden implementation"; }}

Page 6: Java 8 Intro - Core Features

6

Static Methods in interfaces with exampleAnother greate feature delivered by Java 8 is that interfaces can declare (and provide implementation) of static methods. Here is an example:

public interface TimeClient {

static public ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) {

return ZoneId.systemDefault(); }} default public ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } LocalDateTime getLocalDateTime(); // Should be implemented!!!!!!!}

Page 7: Java 8 Intro - Core Features

7

Functional InterfacesIn Java 8 a functional interface is defined as an interface with exactly one abstract method. New annotation @FunctionalInterface

//Invalid@FunctionalInterfacepublic interface SomeInterfaceWithZeroAbstractMethods {}

//Invalid@FunctionalInterfacepublic interface SomeInterfaceWithTWOAbstractMethods { void firstAbstractMethodMethod(); int second(int k);}

@FunctionalInterfacepublic interface SomeInterfaceWithONEAbstractMethods { void f(); // abstract static int g(int k) {return 0;} // static default void h() {} // default}

Page 8: Java 8 Intro - Core Features

8

Built-in functional interfaces @ Java 8Function Method Example

Supplier<T> T get() Supplier<String> stringSupplier = String::new;String newInteger = stringSupplier.get(); // creates new String

Consumer<T> void accept(T t) List<String> one = Arrays.asList("A","AB","ABA","ABBA");Consumer<String> style = (String s) -> System.out.println("Item:"+s);

one.forEach(style);

Predicate<T> boolean test(T t); Predicate<Double> isNegative = x -> x < 0;System.out.println(isNegative.test(new Double(1)));

Function<T,R> R apply(T t); Function<String, Integer> toInteger = Integer::valueOf;Integer test = toInteger.apply("2016");

Page 9: Java 8 Intro - Core Features

9

Lamdas (Lambda expressions)

A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods, that known as functional interfaces.

Core points:

• A lambda expression is a block of code with optional parameters

• Lambda expression is the best choice whenever you want a block of code executed at a later point in time

• Lambda expression reduces amount of code

• Lambda expression used everywhere in Java8 (StreamAPI, Optionals, etc)

Page 10: Java 8 Intro - Core Features

10

Lambda expressions syntax

Lambda Description() -> 4; takes no value and returns 4;x -> 3 * x; takes a number and returns the result of

tripling it(x, y) -> x – y; takes two numbers and returns their

difference(int x, int y) -> x + y; takes two integers and returns their sum

(String s) -> {System.out.print("YES"+s);System.err.print("YES"+s); }

takes a string and prints it to console and err output with “YES” suffix

New keyword -> (minus with bigger, AKA structure dereference in C++) (parameters) ->expression                or(parameters) ->{ statements;}

Lambda Expression and Statements examples:

Page 11: Java 8 Intro - Core Features

11

Method ReferencesList<String> one = Arrays.asList("A","AB","ABA","ABBA");

//Reference to a static methodCollections.sort(one, Comparator.comparing((Function<String, Integer>) (s) -> s.length()));Collections.sort(one, Comparator.comparing(String::length));

List<String> two = Arrays.asList("1","2","3","4");//Reference to an instance method of a particular objecttwo.forEach(System.out::println);two.forEach((x) -> System.out.println(x));

//Reference to an instance method of an arbitrary object of a particular typetwo.forEach(String::toString);two.forEach((s) -> s.toString());

//Reference to a constructorList<Integer> integers = two.stream().map(Integer::new).collect(Collectors.toList());integers = two.stream().map((s) -> new Integer(s)).collect(Collectors.toList());

Page 12: Java 8 Intro - Core Features

12

Streams (Stream API)A newly added Stream API (java.util.stream) introduces real-world functional-style programming into the Java.

A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements.Stream operations are

• intermediate • terminal

Intermediate operations return a stream so we can chain multiple intermediate operations without using semicolons.

Terminal operations are either void or return a non-stream result. In the above example filter, map and sorted are intermediate operations whereas forEach is a terminal operation.

For a full list of all available stream operations see the Stream Javadoc.

Page 13: Java 8 Intro - Core Features

13

• Streams in Collection with some examplesStreams can be created from various data sources ,especially collections

Kind of Streams:• stream() – process elements one by one in single thread• parallelStream() – process elements in several threads

Operation Code Example

Filter (Intermediate)

stringCollection.stream().filter((s) -> s.startsWith(“A"))

Sorted(Intermediate)

stringCollection.parallelStream().sorted((a, b) -> b.compareTo(a));

Map(Intermediate)

stringCollection.stream().map(String::toUpperCase);

Match(Terminal)

stringCollection.stream().anyMatch((s) -> s.startsWith("a"))stringCollection.stream().noneMatch((s) -> s.startsWith("z"))

Count(Terminal)

stringCollection.stream().filter((s) -> s.startsWith(“B")).count();

Page 14: Java 8 Intro - Core Features

14

• Streams in Collection with some examples #2Operation Code Example

Collect(Terminal)

stringCollection.stream().map(String::toUpperCase). .collect(Collectors.toList());

Reduce(Terminal)

List<Integer> list= Arrays.asList(1,2,3,4);System.out.println(list.stream().reduce(0,Integer::sum));

Distinct(Intermediate)

List<Integer> list= Arrays.asList(1,2,3,4,5,4,5);System.out.println(list.stream().distinct().reduce(0,Integer::sum));

Peek(Intermediate)

List<String> list= Arrays.asList(“1”,”2”,”3”,”4”,”5”);list.stream().map(String::toUpperCase).peek((e)->System.out.print(e)).collect(Collectors.toList())

Skip(Intermediate)

List<String> list= Arrays.asList(“1”,”2”,”3”,”4”,”5”);list.stream().map(String::toUpperCase).skip(2).collect(Collectors.toList())

Page 15: Java 8 Intro - Core Features

15

Optional vs NullPointerExceptionThe … NullPointerException is by far the most popular cause of Java application failures.

Long time ago the great Google Guava project introduced the Optional as a solution to NullPointerException, discouraging codebase pollution with null checks and encouraging developers to write cleaner code.

Inspired by Google Guava, the Optional is now a part of Java 8 library

Here is basic example

Optional<String> optional = Optional.of("Some value");

optional.isPresent(); // trueoptional.get(); // "Some value"optional.orElse("fallback"); // "Some value"

optional.ifPresent((s) -> System.out.println(s.charAt(2))); // “m"

Page 16: Java 8 Intro - Core Features

16

Questions?

Email:[email protected]

Page 17: Java 8 Intro - Core Features