SlideShare uma empresa Scribd logo
1 de 69
Baixar para ler offline
Java 8 Features with Examples
Presented By: Gaurav And Ashish
Agenda New Features in Java language
● forEach() method in Iterable interface
● Interface’s default and Static Methods
● Functional Interface
● Lambda Expression
● Method References
New Features in Java libraries
● Stream API
● Date/Time API
For Each() Method in iterable interface
Java 8 has introduced forEach method in java.lang.Iterable interface so
that while writing code we focus on business logic only. forEach method
takes java.util.function.Consumer object as argument, so it helps in
having our business logic at a separate location that we can reuse.
Let’s see forEach usage with simple example.
Example
//creating sample Collection
List<Integer> myList = new ArrayList<Integer>();
for(int i=0; i<10; i++) myList.add(i);
//traversing using Iterator
Iterator<Integer> it = myList.iterator();
while(it.hasNext()){
Integer i = it.next();
System.out.println("Iterator Value::"+i);
}
To Be Continued….
//traversing through forEach method of Iterable with anonymous class
myList.forEach(new Consumer<Integer>() {
public void accept(Integer t) {
System.out.println("forEach anonymous class Value::"+t);
}
});
//traversing with Consumer interface implementation
MyConsumer action = new MyConsumer();
myList.forEach(action);
//Consumer implementation that can be reused
class MyConsumer implements Consumer<Integer>{
public void accept(Integer t) {
System.out.println("Consumer impl Value::"+t);
}
}
Default And static methods in Interface
● Interfaces are enhanced to have method with implementation.
● We can use default and static keyword to create interfaces with
method implementation.
forEach method implementation in Iterable interface is:
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
Default And static methods in Interface
Syntax
Default methods in Interface
● Classes implement interface that contains a default method
○ Not override the default method and will inherit the default method
○ Override the default method similar to other methods we override in subclass.
○ Redeclare default method as abstract, which force subclass to override it.
Default methods in Interface
● Solve the conflict when the same method declared in interface or
class
○ Method of Superclasses, if a superclass provides a concrete
method.
○ If a superinterface provides a default method, and another
interface supplies a method with the same name and parameter
types (default or not), then you must override that method.
Static methods in Interface
● Similar to default methods except that we can’t override them in the
implementation classes
Demonstration
Functional interface
What is functional interface?
Functional interface
● New term of Java 8
● A functional interface is an interface with only one abstract method.
Functional interface
● Methods from the Object class don’t count.
Functional interface
Annotation in Functional
Interface
Functional interface
● A functional interface can be annotated.
● It’s optional.
Functional interface
● Show compile error when define more than one abstract method.
Functional interface
Functional Interfaces
Toolbox
Functional interface
● Brand new java.util.function package.
● Rich set of function interfaces.
● 4 categories:
○ Supplier
○ Consumer
○ Predicate
○ Function
Consumer interface
● Accept an object and return nothing.
● Can be implemented by lambda expression.
Demonstration
Lambda Expression
Lambda Expression are the way through which we can visualize functional programming in the
java object oriented world. Objects are the base of java programming language and we can
never have a function without an Object, that’s why Java language provide support for using
lambda expressions only with functional interfaces.
Since there is only one abstract function in the functional interfaces, there is no confusion in
applying the lambda expression to the method. Lambda Expressions syntax is (argument) ->
(body). Now let’s see how we can write above anonymous Runnable using lambda expression.
Runnable r1 = () -> System.out.println("My Runnable");
Lambda Expression
● Let’s try to understand what is happening in the lambda expression
above.
○ Runnable is a functional interface, that’s why we can use lambda exp
create it’s instance.
○ Since run() method takes no argument, our lambda expression also
argument.
○ Just like if-else blocks, we can avoid curly braces ({}) since we have
statement in the method body. For multiple statements, we would h
braces like any other methods.
Lambda Expression
● A block of code that you can pass around so it can be executed later, once or multiple
times.
● Anonymous methods.
● Reduce verbosity caused by anonymous classes.
● Alonzo Church in his invention of the lambda calculus in 1936. - Lisp 1958 - Supported in
C#, JavaScript,Python, Ruby, C++11
Lambda Expression
Example 1:
Runnable r = new Runnable(){
@Override
public void run() {
System.out.println("My Runnable");
}};
Example 2:
Comparator<Person> byAge = new Comparator<Person>(){
public int compare(Person p1, Person p2){
return p1.getAge().compareTo(p2.getAge());
}
};
Lambda Expression
Example 1: with lambda
Runnable r1 = () -> System.out.println("My Runnable");
Example 2: with lambda
Comparator<Person> byAge =
(Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());
Examples of Java 8 Lambdas
● A Java 8 lambda is basically a method in Java without a declaration usually written as
(parameters) -> { body }. Examples,
(int x, int y) -> { return x + y; }
x -> x * x
( ) -> x
● A lambda can have zero or more parameters separated by commas and their type can
be explicitly declared or inferred from the context.
● Parenthesis are not needed around a single parameter.
● ( ) is used to denote zero parameters.
● The body can contain zero or more statements.
● Braces are not needed around a single-statement body.
Print a list of integers with a lambda
List<Integer> intSeq = Arrays.asList(1,2,3);
intSeq.forEach(x -> System.out.println(x));
•x -> System.out.println(x) is a lambda expression that defines an
anonymous function with one parameter named x of type Integer
A multiline lambda
List<Integer> intSeq = Arrays.asList(1,2,3);
intSeq.forEach(x -> {
x += 2;
System.out.println(x);
});
•Braces are needed to enclose a multiline body in a lambda
expression.
A lambda with a defined local variable
List<Integer> intSeq = Arrays.asList(1,2,3);
intSeq.forEach(x -> {
int y = x * 2;
System.out.println(y);
});
•Just as with ordinary functions, you can define local variables inside
the body of a lambda expression
A lambda with a declared parameter type
List<Integer> intSeq = Arrays.asList(1,2,3);
intSeq.forEach((Integer x -> {
x += 2;
System.out.println(x);
});
•You can, if you wish, specify the parameter type.
A lambda with a declared parameter type
public class LVCExample {
public static void main(String[] args) {
List<Integer> intSeq = Arrays.asList(1,2,3);
int var = 10;
intSeq.forEach(x -> System.out.println(x + var));
}
}
local variables used inside the body of a lambda must be final or
effectively final
Method Reference
● Method reference is an important feature related to lambda expressions. In order to that a
method reference requires a target type context that consists of a compatible functional
interface.
Method Reference
● There are four kinds of method references:
○ Reference to a static method
○ Reference to an instance method of a particular object
○ Reference to an instance method of an arbitrary object of a particular type
○ Reference to a constructor
Method Reference
● Reference to a static method
The syntax: ContainingClass::staticMethodName
Method Reference
● Reference to an instance method of a particular object
The syntax: containingObject::instanceMethodName
Method Reference
● Reference to an instance method of an arbitrary object of a particular
type.
The syntax: ContainingType::methodName
Method Reference
● Reference to a constructor
The syntax: ClassName::new
Method Reference
● Method references as Lambdas Expressions
Demonstration
Stream
Before we look into Java Stream API Examples, let’s see why it was
required. Suppose we want to iterate over a list of integers and find out
sum of all the integers greater than 10.
Prior to Java 8, the approach to do it would be:
private static int sumIterator(List<Integer> list) {
Iterator<Integer> it = list.iterator();
int sum = 0;
while (it.hasNext()) {
int num = it.next();
if (num > 10) {
sum += num;
}
}
return sum;
}
Stream
There are three major problems with the previous approach:
1. We just want to know the sum of integers but we would also have
to provide how the iteration will take place, this is also called
external iteration because client program is handling the algorithm
to iterate over the list.
2. The program is sequential in nature, there is no way we can do
this in parallel easily.
3. There is a lot of code to do even a simple task.
Stream
1. To overcome all the above shortcomings, Java 8 Stream API was introduced. We can use
Java Stream API to implement internal iteration, that is better because java framework is
in control of the iteration.
2. Internal iteration provides several features such as sequential and parallel execution,
filtering based on the given criteria, mapping etc.
3. Most of the Java 8 Stream API method arguments are functional interfaces, so lambda
expressions work very well with them. Let’s see how can we write above logic in a single
line statement using Java Streams.
private static int sumStream(List<Integer> list) {
return list.stream().filter(i -> i > 10).mapToInt(i -> i).sum();
}
What exactly is Stream ?
“A sequence of elements from a source that supports data processing
operations”
● Sequence of elements— Collections are data structures, they’re mostly about storing
and accessing elements with specific time/space complexities. But streams are about
expressing computations such as filter, sorted, and map. Collections are about data;
streams are about computations.
● Source— Streams consume from a data-providing source such as collections, arrays, or
I/O resources. Note that generating a stream from an ordered collection preserves the
ordering. The elements of a stream coming from a list will have the same order as the list.
● Data processing operations— Streams support database-like operations and common
operations from functional programming languages to manipulate data, such as filter,
map, reduce, find, match, sort, and so on. Stream operations can be executed either
sequentially or in parallel
Collections and java Stream
● Java Stream is a data structure that is computed on-demand.
● Java Stream doesn’t store data,.
● Can easily be outputted as arrays or lists
● Java Stream operations use functional interfaces.
● Java 8 Stream internal iteration principle helps in achieving
lazy-seeking in some of the stream operations.
● Java Streams are consumable.
● Java 8 Stream support sequential as well as parallel processing,
Stream-Build Streams
● Build streams from collections
List<Dish> dishes = new ArrayList<Dish>();
....(add some Dishes)....
Stream<Dish> stream = dishes.stream();
Stream-Build Streams
● Build streams from arrays
Integer[] integers =
{1, 2, 3, 4, 5, 6, 7, 8, 9};
Stream<Integer> stream = Stream.of(integers);
Function Package In Java 8
● Function package contains Functional interfaces.
● Some of the main functional interfaces are :-
○ Function interface
○ Predicate interface
○ Consumer interface
○ Supplier interface
Function And BiFunction interface
● Function represents a function that takes one type of argument and returns another
type of argument. Function<T, R> is the generic form where T is the type of the input
to the function and R is the type of the result of the function.
● For handling primitive types, there are specific Function interfaces – ToIntFunction,
ToLongFunction, ToDoubleFunction, ToIntBiFunction, ToLongBiFunction,
ToDoubleBiFunction, LongToIntFunction, LongToDoubleFunction, IntToLongFunction,
IntToDoubleFunction etc.
● We can apply our business logic with that value and return the result. Function has
function method as apply(T t) which accepts one argument.
import java.util.function.Function;
public class FunctionDemo {
public static void main(String[] args) {
Function<Integer, String> function = (num1) -> "Result is: ” + num1;
System.out.println(function.apply(20));
}
}
Function And BiFunction interface
● Some of the Stream methods where Function or it’s primitive specialization is used are:
○ <R> Stream<R> map(Function<? super T, ? extends R> mapper)
○ IntStream mapToInt(ToIntFunction<? super T> mapper) – similarly
double returning primitive specific stream.
○ IntStream flatMapToInt(Function<? super T, ? extends IntStream> m
similarly for long and double
○ <A> A[] toArray(IntFunction<A[]> generator)
○ <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner)
Predicate And BiPredicate Interface
● It represents a predicate against which elements of the stream are tested. This is used
to filter elements from the java stream.
● Just like Function, there are primitive specific interfaces for int, long and double.
● Predicate functional method is test(Object). Find the simple example for how to use
Predicate.
public class BiPredicateDemo {
public static void main(String[] args){
BiPredicate<Integer, String> condition = (i,s)-> i>20 && s.startsWith("R");
System.out.println(condition.test(10,"Ram"));
System.out.println(condition.test(30,"Shyam"));
System.out.println(condition.test(30,"Ram"));
}
}
Predicate And BiPredicate Interface
● Some of the Stream methods where Predicate or BiPredicate specializations are used
are:
○ Stream<T> filter(Predicate<? super T> predicate)
○ boolean anyMatch(Predicate<? super T> predicate)
○ boolean allMatch(Predicate<? super T> predicate)
○ boolean noneMatch(Predicate<? super T> predicate)
Consumer And BiConsumer Interface
● It represents an operation that accepts a single input argument and returns no result.
● It can be used to perform some action on all the elements of the java stream.
● Consumer functional method is accept(Object, Object). This methods performs the
operation defined by BiConsumer.
public class ConsumerExample {
static void print(String name){
System.out.println("Hello "+name);
}
}
public static void main(String[] args) {
// Referring method to String type Consumer interface
Consumer<String>consumer1=ConsumerExample::print;
consumer1.accept("John");
Consumer And BiConsumer Interface
● Some of the Java 8 Stream methods where Consumer, BiConsumer or it’s primitive
specialization interfaces are used are
○ Stream<T> peek(Consumer<? super T> action)
○ void forEach(Consumer<? super T> action)
○ void forEachOrdered(Consumer<? super T> action)
Supplier Interface
● Supplier represent an operation through which we can generate new values in the
stream.
● A supplier of objects. The result objects are either created during the invocation of
get() or by some prior action..
● And the sole method in this interface is get() which should return an object of the
declared type
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class TestSupplier {
public static void main(String[] args) {
Supplier < Person > supplier = () - > {
return new Person("Varun");
};
Person p = supplier.get();
System.out.println("Person Detail:n" +
p.getName());
}
}
Supplier Interface
● Some of the methods in Stream that takes Supplier argument are:
○ public static<T> Stream<T> generate(Supplier<T> s)
○ <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T>
accumulator,BiConsumer<R, R> combiner)
Stream-java.util.optional
● Java Optional is a container object which may or may not contain a non-null value. If a
value is present, isPresent() will return true and get() will return the value. Stream
terminal operations return Optional object. Some of these methods are:
○ Optional<T> reduce(BinaryOperator<T> accumulator)
○ Optional<T> min(Comparator<? super T> comparator)
○ Optional<T> max(Comparator<? super T> comparator)
○ Optional<T> findFirst()
○ Optional<T> findAny()
Stream-operations
● Intermediate operations (return Stream)
Java Stream API operations that returns a new Stream are called intermediate
operations. Most of the times, these operations are lazy in nature, so they start
producing new stream elements and send it to the next operation. Intermediate
operations are never the final result producing operations. Commonly used
intermediate operations are filter and map.
Stream-operations
● Terminal operations
Java 8 Stream API operations that returns a result or produce a side effect. Once the
terminal method is called on a stream, it consumes the stream and after that we
can’t use stream. Terminal operations are eager in nature i.e they process all the
elements in the stream before returning the result. Commonly used terminal
methods are forEach, toArray, min, max, findFirst, anyMatch, allMatch etc. You can
identify terminal methods from the return type, they will never return a Stream.
Stream-operations
● Short circuiting operations
An intermediate operation is called short circuiting, if it may produce finite stream for an
infinite stream. For example limit() and skip() are two short circuiting intermediate operations.
A terminal operation is called short circuiting, if it may terminate in finite time for infinite
stream. For example anyMatch, allMatch, noneMatch, findFirst and findAny are short circuiting
terminal operations.
Demonstration
Date ANd Time API
Why do we need new Java Date Time API?
1. Java Date Time classes are not defined consistently, we have Date Class in both java.util as well as
java.sql packages. Again formatting and parsing classes are defined in java.text package.
2. java.util.Date contains both date and time, whereas java.sql.Date contains only date. Having this in
java.sql package doesn’t make sense. Also both the classes have same name, that is a very bad design
itself.
3. There are no clearly defined classes for time, timestamp, formatting and parsing. We have
java.text.DateFormat abstract class for parsing and formatting need. Usually SimpleDateFormat class is
used for parsing and formatting.
4. All the Date classes are mutable, so they are not thread safe. It’s one of the biggest problem with Java
Date and Calendar classes.
5. Date class doesn’t provide internationalization, there is no timezone support. So java.util.Calendar and
java.util.TimeZone classes were introduced, but they also have all the problems listed above.
Date ANd Time API
Java 8 Date Time API is JSR-310 implementation. It is designed to overcome all the flaws in the
legacy date time implementations. Some of the design principles of new Date Time API are:
1. Immutability: All the classes in the new Date Time API are immutable and good for multithreaded
environments.
2. Separation of Concerns: The new API separates clearly between human readable date time and
machine time (unix timestamp). It defines separate classes for Date, Time, DateTime, Timestamp,
Timezone etc.
3. Clarity: The methods are clearly defined and perform the same action in all the classes. For example,
to get the current instance we have now() method. There are format() and parse() methods defined in
all these classes rather than having a separate class for them.
4. Utility operations: All the new Date Time API classes comes with methods to perform common tasks,
such as plus, minus, format, parsing, getting separate part in date/time etc.
5. Extendable: The new Date Time API works on ISO-8601 calendar system but we can use it with other
non ISO calendars as well.
Java 8 Date ANd Time Packages
Java 8 Date Time API consists of following packages.
1. java.time Package: This is the base package of new Java Date Time API. such as
LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration etc. All of these classes
are immutable and thread safe.
2. java.time.chrono Package: This package defines generic APIs for non ISO calendar
systems. We can extend AbstractChronology class to create our own calendar system.
3. java.time.temporal Package: This package contains temporal objects and we can use it
for find out specific date or time related to date/time object. For example, we can use
these to find out the first or last day of the month. You can identify these methods easily
because they always have format “withXXX”.
4. java.time.zone Package: This package contains classes for supporting different time
zones and their rules.
Demonstration
References
❖ Java Platform Standard Edition 8 Documentation,
(http://docs.oracle.com/javase/8/docs/)
❖ Java 8 In Action, Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft.
❖ Beginning Java 8 Language Features, Kishori Sharan.
❖ What’s new in Java 8 - Pluralsight
❖ http://www.journaldev.com/2389/java-8-features-with-examples
Sample Implementation
git@github.com:NexThoughts/java8.git
The End

Mais conteúdo relacionado

Mais procurados

Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Java11 New Features
Java11 New FeaturesJava11 New Features
Java11 New FeaturesHaim Michael
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11 Knoldus Inc.
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 

Mais procurados (20)

Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Java11 New Features
Java11 New FeaturesJava11 New Features
Java11 New Features
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Generics
GenericsGenerics
Generics
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 

Destaque (17)

Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Spring Web Flow
Spring Web FlowSpring Web Flow
Spring Web Flow
 
JFree chart
JFree chartJFree chart
JFree chart
 
Introduction to thymeleaf
Introduction to thymeleafIntroduction to thymeleaf
Introduction to thymeleaf
 
Progressive Web-App (PWA)
Progressive Web-App (PWA)Progressive Web-App (PWA)
Progressive Web-App (PWA)
 
Jmh
JmhJmh
Jmh
 
Introduction to gradle
Introduction to gradleIntroduction to gradle
Introduction to gradle
 
Hamcrest
HamcrestHamcrest
Hamcrest
 
Introduction to es6
Introduction to es6Introduction to es6
Introduction to es6
 
Grails with swagger
Grails with swaggerGrails with swagger
Grails with swagger
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Apache tika
Apache tikaApache tika
Apache tika
 
Cosmos DB Service
Cosmos DB ServiceCosmos DB Service
Cosmos DB Service
 
Jsoup
JsoupJsoup
Jsoup
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Vertx
VertxVertx
Vertx
 

Semelhante a Java 8 features

Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalUrs Peter
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Raffi Khatchadourian
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 

Semelhante a Java 8 features (20)

Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Java 8
Java 8Java 8
Java 8
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
java8
java8java8
java8
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
Java8
Java8Java8
Java8
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java 8
Java 8Java 8
Java 8
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Java 8
Java 8Java 8
Java 8
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 

Mais de NexThoughts Technologies (20)

Alexa skill
Alexa skillAlexa skill
Alexa skill
 
GraalVM
GraalVMGraalVM
GraalVM
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Apache commons
Apache commonsApache commons
Apache commons
 
HazelCast
HazelCastHazelCast
HazelCast
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Swagger
SwaggerSwagger
Swagger
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Arango DB
Arango DBArango DB
Arango DB
 
Jython
JythonJython
Jython
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
 
Ethereum
EthereumEthereum
Ethereum
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Google authentication
Google authenticationGoogle authentication
Google authentication
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Último (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Java 8 features

  • 1. Java 8 Features with Examples Presented By: Gaurav And Ashish
  • 2. Agenda New Features in Java language ● forEach() method in Iterable interface ● Interface’s default and Static Methods ● Functional Interface ● Lambda Expression ● Method References New Features in Java libraries ● Stream API ● Date/Time API
  • 3. For Each() Method in iterable interface Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic only. forEach method takes java.util.function.Consumer object as argument, so it helps in having our business logic at a separate location that we can reuse. Let’s see forEach usage with simple example.
  • 4. Example //creating sample Collection List<Integer> myList = new ArrayList<Integer>(); for(int i=0; i<10; i++) myList.add(i); //traversing using Iterator Iterator<Integer> it = myList.iterator(); while(it.hasNext()){ Integer i = it.next(); System.out.println("Iterator Value::"+i); } To Be Continued….
  • 5. //traversing through forEach method of Iterable with anonymous class myList.forEach(new Consumer<Integer>() { public void accept(Integer t) { System.out.println("forEach anonymous class Value::"+t); } }); //traversing with Consumer interface implementation MyConsumer action = new MyConsumer(); myList.forEach(action); //Consumer implementation that can be reused class MyConsumer implements Consumer<Integer>{ public void accept(Integer t) { System.out.println("Consumer impl Value::"+t); } }
  • 6. Default And static methods in Interface ● Interfaces are enhanced to have method with implementation. ● We can use default and static keyword to create interfaces with method implementation. forEach method implementation in Iterable interface is: default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
  • 7. Default And static methods in Interface Syntax
  • 8. Default methods in Interface ● Classes implement interface that contains a default method ○ Not override the default method and will inherit the default method ○ Override the default method similar to other methods we override in subclass. ○ Redeclare default method as abstract, which force subclass to override it.
  • 9. Default methods in Interface ● Solve the conflict when the same method declared in interface or class ○ Method of Superclasses, if a superclass provides a concrete method. ○ If a superinterface provides a default method, and another interface supplies a method with the same name and parameter types (default or not), then you must override that method.
  • 10. Static methods in Interface ● Similar to default methods except that we can’t override them in the implementation classes
  • 12. Functional interface What is functional interface?
  • 13. Functional interface ● New term of Java 8 ● A functional interface is an interface with only one abstract method.
  • 14. Functional interface ● Methods from the Object class don’t count.
  • 15. Functional interface Annotation in Functional Interface
  • 16. Functional interface ● A functional interface can be annotated. ● It’s optional.
  • 17. Functional interface ● Show compile error when define more than one abstract method.
  • 19. Functional interface ● Brand new java.util.function package. ● Rich set of function interfaces. ● 4 categories: ○ Supplier ○ Consumer ○ Predicate ○ Function
  • 20. Consumer interface ● Accept an object and return nothing. ● Can be implemented by lambda expression.
  • 22. Lambda Expression Lambda Expression are the way through which we can visualize functional programming in the java object oriented world. Objects are the base of java programming language and we can never have a function without an Object, that’s why Java language provide support for using lambda expressions only with functional interfaces. Since there is only one abstract function in the functional interfaces, there is no confusion in applying the lambda expression to the method. Lambda Expressions syntax is (argument) -> (body). Now let’s see how we can write above anonymous Runnable using lambda expression. Runnable r1 = () -> System.out.println("My Runnable");
  • 23. Lambda Expression ● Let’s try to understand what is happening in the lambda expression above. ○ Runnable is a functional interface, that’s why we can use lambda exp create it’s instance. ○ Since run() method takes no argument, our lambda expression also argument. ○ Just like if-else blocks, we can avoid curly braces ({}) since we have statement in the method body. For multiple statements, we would h braces like any other methods.
  • 24. Lambda Expression ● A block of code that you can pass around so it can be executed later, once or multiple times. ● Anonymous methods. ● Reduce verbosity caused by anonymous classes. ● Alonzo Church in his invention of the lambda calculus in 1936. - Lisp 1958 - Supported in C#, JavaScript,Python, Ruby, C++11
  • 25. Lambda Expression Example 1: Runnable r = new Runnable(){ @Override public void run() { System.out.println("My Runnable"); }}; Example 2: Comparator<Person> byAge = new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.getAge().compareTo(p2.getAge()); } };
  • 26. Lambda Expression Example 1: with lambda Runnable r1 = () -> System.out.println("My Runnable"); Example 2: with lambda Comparator<Person> byAge = (Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());
  • 27. Examples of Java 8 Lambdas ● A Java 8 lambda is basically a method in Java without a declaration usually written as (parameters) -> { body }. Examples, (int x, int y) -> { return x + y; } x -> x * x ( ) -> x ● A lambda can have zero or more parameters separated by commas and their type can be explicitly declared or inferred from the context. ● Parenthesis are not needed around a single parameter. ● ( ) is used to denote zero parameters. ● The body can contain zero or more statements. ● Braces are not needed around a single-statement body.
  • 28. Print a list of integers with a lambda List<Integer> intSeq = Arrays.asList(1,2,3); intSeq.forEach(x -> System.out.println(x)); •x -> System.out.println(x) is a lambda expression that defines an anonymous function with one parameter named x of type Integer
  • 29. A multiline lambda List<Integer> intSeq = Arrays.asList(1,2,3); intSeq.forEach(x -> { x += 2; System.out.println(x); }); •Braces are needed to enclose a multiline body in a lambda expression.
  • 30. A lambda with a defined local variable List<Integer> intSeq = Arrays.asList(1,2,3); intSeq.forEach(x -> { int y = x * 2; System.out.println(y); }); •Just as with ordinary functions, you can define local variables inside the body of a lambda expression
  • 31. A lambda with a declared parameter type List<Integer> intSeq = Arrays.asList(1,2,3); intSeq.forEach((Integer x -> { x += 2; System.out.println(x); }); •You can, if you wish, specify the parameter type.
  • 32. A lambda with a declared parameter type public class LVCExample { public static void main(String[] args) { List<Integer> intSeq = Arrays.asList(1,2,3); int var = 10; intSeq.forEach(x -> System.out.println(x + var)); } } local variables used inside the body of a lambda must be final or effectively final
  • 33. Method Reference ● Method reference is an important feature related to lambda expressions. In order to that a method reference requires a target type context that consists of a compatible functional interface.
  • 34. Method Reference ● There are four kinds of method references: ○ Reference to a static method ○ Reference to an instance method of a particular object ○ Reference to an instance method of an arbitrary object of a particular type ○ Reference to a constructor
  • 35. Method Reference ● Reference to a static method The syntax: ContainingClass::staticMethodName
  • 36. Method Reference ● Reference to an instance method of a particular object The syntax: containingObject::instanceMethodName
  • 37. Method Reference ● Reference to an instance method of an arbitrary object of a particular type. The syntax: ContainingType::methodName
  • 38. Method Reference ● Reference to a constructor The syntax: ClassName::new
  • 39. Method Reference ● Method references as Lambdas Expressions
  • 41. Stream Before we look into Java Stream API Examples, let’s see why it was required. Suppose we want to iterate over a list of integers and find out sum of all the integers greater than 10. Prior to Java 8, the approach to do it would be: private static int sumIterator(List<Integer> list) { Iterator<Integer> it = list.iterator(); int sum = 0; while (it.hasNext()) { int num = it.next(); if (num > 10) { sum += num; } } return sum; }
  • 42. Stream There are three major problems with the previous approach: 1. We just want to know the sum of integers but we would also have to provide how the iteration will take place, this is also called external iteration because client program is handling the algorithm to iterate over the list. 2. The program is sequential in nature, there is no way we can do this in parallel easily. 3. There is a lot of code to do even a simple task.
  • 43. Stream 1. To overcome all the above shortcomings, Java 8 Stream API was introduced. We can use Java Stream API to implement internal iteration, that is better because java framework is in control of the iteration. 2. Internal iteration provides several features such as sequential and parallel execution, filtering based on the given criteria, mapping etc. 3. Most of the Java 8 Stream API method arguments are functional interfaces, so lambda expressions work very well with them. Let’s see how can we write above logic in a single line statement using Java Streams. private static int sumStream(List<Integer> list) { return list.stream().filter(i -> i > 10).mapToInt(i -> i).sum(); }
  • 44. What exactly is Stream ? “A sequence of elements from a source that supports data processing operations” ● Sequence of elements— Collections are data structures, they’re mostly about storing and accessing elements with specific time/space complexities. But streams are about expressing computations such as filter, sorted, and map. Collections are about data; streams are about computations. ● Source— Streams consume from a data-providing source such as collections, arrays, or I/O resources. Note that generating a stream from an ordered collection preserves the ordering. The elements of a stream coming from a list will have the same order as the list. ● Data processing operations— Streams support database-like operations and common operations from functional programming languages to manipulate data, such as filter, map, reduce, find, match, sort, and so on. Stream operations can be executed either sequentially or in parallel
  • 45. Collections and java Stream ● Java Stream is a data structure that is computed on-demand. ● Java Stream doesn’t store data,. ● Can easily be outputted as arrays or lists ● Java Stream operations use functional interfaces. ● Java 8 Stream internal iteration principle helps in achieving lazy-seeking in some of the stream operations. ● Java Streams are consumable. ● Java 8 Stream support sequential as well as parallel processing,
  • 46. Stream-Build Streams ● Build streams from collections List<Dish> dishes = new ArrayList<Dish>(); ....(add some Dishes).... Stream<Dish> stream = dishes.stream();
  • 47. Stream-Build Streams ● Build streams from arrays Integer[] integers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Stream<Integer> stream = Stream.of(integers);
  • 48. Function Package In Java 8 ● Function package contains Functional interfaces. ● Some of the main functional interfaces are :- ○ Function interface ○ Predicate interface ○ Consumer interface ○ Supplier interface
  • 49. Function And BiFunction interface ● Function represents a function that takes one type of argument and returns another type of argument. Function<T, R> is the generic form where T is the type of the input to the function and R is the type of the result of the function. ● For handling primitive types, there are specific Function interfaces – ToIntFunction, ToLongFunction, ToDoubleFunction, ToIntBiFunction, ToLongBiFunction, ToDoubleBiFunction, LongToIntFunction, LongToDoubleFunction, IntToLongFunction, IntToDoubleFunction etc. ● We can apply our business logic with that value and return the result. Function has function method as apply(T t) which accepts one argument. import java.util.function.Function; public class FunctionDemo { public static void main(String[] args) { Function<Integer, String> function = (num1) -> "Result is: ” + num1; System.out.println(function.apply(20)); } }
  • 50. Function And BiFunction interface ● Some of the Stream methods where Function or it’s primitive specialization is used are: ○ <R> Stream<R> map(Function<? super T, ? extends R> mapper) ○ IntStream mapToInt(ToIntFunction<? super T> mapper) – similarly double returning primitive specific stream. ○ IntStream flatMapToInt(Function<? super T, ? extends IntStream> m similarly for long and double ○ <A> A[] toArray(IntFunction<A[]> generator) ○ <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)
  • 51. Predicate And BiPredicate Interface ● It represents a predicate against which elements of the stream are tested. This is used to filter elements from the java stream. ● Just like Function, there are primitive specific interfaces for int, long and double. ● Predicate functional method is test(Object). Find the simple example for how to use Predicate. public class BiPredicateDemo { public static void main(String[] args){ BiPredicate<Integer, String> condition = (i,s)-> i>20 && s.startsWith("R"); System.out.println(condition.test(10,"Ram")); System.out.println(condition.test(30,"Shyam")); System.out.println(condition.test(30,"Ram")); } }
  • 52. Predicate And BiPredicate Interface ● Some of the Stream methods where Predicate or BiPredicate specializations are used are: ○ Stream<T> filter(Predicate<? super T> predicate) ○ boolean anyMatch(Predicate<? super T> predicate) ○ boolean allMatch(Predicate<? super T> predicate) ○ boolean noneMatch(Predicate<? super T> predicate)
  • 53. Consumer And BiConsumer Interface ● It represents an operation that accepts a single input argument and returns no result. ● It can be used to perform some action on all the elements of the java stream. ● Consumer functional method is accept(Object, Object). This methods performs the operation defined by BiConsumer. public class ConsumerExample { static void print(String name){ System.out.println("Hello "+name); } } public static void main(String[] args) { // Referring method to String type Consumer interface Consumer<String>consumer1=ConsumerExample::print; consumer1.accept("John");
  • 54. Consumer And BiConsumer Interface ● Some of the Java 8 Stream methods where Consumer, BiConsumer or it’s primitive specialization interfaces are used are ○ Stream<T> peek(Consumer<? super T> action) ○ void forEach(Consumer<? super T> action) ○ void forEachOrdered(Consumer<? super T> action)
  • 55. Supplier Interface ● Supplier represent an operation through which we can generate new values in the stream. ● A supplier of objects. The result objects are either created during the invocation of get() or by some prior action.. ● And the sole method in this interface is get() which should return an object of the declared type public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } } public class TestSupplier { public static void main(String[] args) { Supplier < Person > supplier = () - > { return new Person("Varun"); }; Person p = supplier.get(); System.out.println("Person Detail:n" + p.getName()); } }
  • 56. Supplier Interface ● Some of the methods in Stream that takes Supplier argument are: ○ public static<T> Stream<T> generate(Supplier<T> s) ○ <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner)
  • 57. Stream-java.util.optional ● Java Optional is a container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. Stream terminal operations return Optional object. Some of these methods are: ○ Optional<T> reduce(BinaryOperator<T> accumulator) ○ Optional<T> min(Comparator<? super T> comparator) ○ Optional<T> max(Comparator<? super T> comparator) ○ Optional<T> findFirst() ○ Optional<T> findAny()
  • 58. Stream-operations ● Intermediate operations (return Stream) Java Stream API operations that returns a new Stream are called intermediate operations. Most of the times, these operations are lazy in nature, so they start producing new stream elements and send it to the next operation. Intermediate operations are never the final result producing operations. Commonly used intermediate operations are filter and map.
  • 59. Stream-operations ● Terminal operations Java 8 Stream API operations that returns a result or produce a side effect. Once the terminal method is called on a stream, it consumes the stream and after that we can’t use stream. Terminal operations are eager in nature i.e they process all the elements in the stream before returning the result. Commonly used terminal methods are forEach, toArray, min, max, findFirst, anyMatch, allMatch etc. You can identify terminal methods from the return type, they will never return a Stream.
  • 60. Stream-operations ● Short circuiting operations An intermediate operation is called short circuiting, if it may produce finite stream for an infinite stream. For example limit() and skip() are two short circuiting intermediate operations. A terminal operation is called short circuiting, if it may terminate in finite time for infinite stream. For example anyMatch, allMatch, noneMatch, findFirst and findAny are short circuiting terminal operations.
  • 62. Date ANd Time API Why do we need new Java Date Time API? 1. Java Date Time classes are not defined consistently, we have Date Class in both java.util as well as java.sql packages. Again formatting and parsing classes are defined in java.text package. 2. java.util.Date contains both date and time, whereas java.sql.Date contains only date. Having this in java.sql package doesn’t make sense. Also both the classes have same name, that is a very bad design itself. 3. There are no clearly defined classes for time, timestamp, formatting and parsing. We have java.text.DateFormat abstract class for parsing and formatting need. Usually SimpleDateFormat class is used for parsing and formatting. 4. All the Date classes are mutable, so they are not thread safe. It’s one of the biggest problem with Java Date and Calendar classes. 5. Date class doesn’t provide internationalization, there is no timezone support. So java.util.Calendar and java.util.TimeZone classes were introduced, but they also have all the problems listed above.
  • 63. Date ANd Time API Java 8 Date Time API is JSR-310 implementation. It is designed to overcome all the flaws in the legacy date time implementations. Some of the design principles of new Date Time API are: 1. Immutability: All the classes in the new Date Time API are immutable and good for multithreaded environments. 2. Separation of Concerns: The new API separates clearly between human readable date time and machine time (unix timestamp). It defines separate classes for Date, Time, DateTime, Timestamp, Timezone etc. 3. Clarity: The methods are clearly defined and perform the same action in all the classes. For example, to get the current instance we have now() method. There are format() and parse() methods defined in all these classes rather than having a separate class for them. 4. Utility operations: All the new Date Time API classes comes with methods to perform common tasks, such as plus, minus, format, parsing, getting separate part in date/time etc. 5. Extendable: The new Date Time API works on ISO-8601 calendar system but we can use it with other non ISO calendars as well.
  • 64. Java 8 Date ANd Time Packages Java 8 Date Time API consists of following packages. 1. java.time Package: This is the base package of new Java Date Time API. such as LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration etc. All of these classes are immutable and thread safe. 2. java.time.chrono Package: This package defines generic APIs for non ISO calendar systems. We can extend AbstractChronology class to create our own calendar system. 3. java.time.temporal Package: This package contains temporal objects and we can use it for find out specific date or time related to date/time object. For example, we can use these to find out the first or last day of the month. You can identify these methods easily because they always have format “withXXX”. 4. java.time.zone Package: This package contains classes for supporting different time zones and their rules.
  • 66.
  • 67. References ❖ Java Platform Standard Edition 8 Documentation, (http://docs.oracle.com/javase/8/docs/) ❖ Java 8 In Action, Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft. ❖ Beginning Java 8 Language Features, Kishori Sharan. ❖ What’s new in Java 8 - Pluralsight ❖ http://www.journaldev.com/2389/java-8-features-with-examples