SlideShare a Scribd company logo
Exploring Streams and Lambdas in
Java8
• Presented By:Isuru Samaraweera
Agenda
• Why change Java again?
• What is FP & Lambda?
• Functional Interfaces
• Streams
• Reduction
• Overloading Lambdas
• Advanced Collections and collectors
• Partioning and Grouping data
• Data Parrellelism
• Testing Lambdas
Why change java again
• Rise of the multicore CPUS
• Algorithms involves locks error-prone time
consuming
• Util.concurrent libraries have limititaions
• Lack of efficient parrelel operations on a
collection
• Java8 allows complex collection-processing
algorithms
What is fp
• Oop data abstraction/side efects
• Functional focuses on side effect free
• Pure functions/lambdas
• Pass functions around easeir to write lazy code
which initialises values when necessary
• n -> n % 2 != 0;
• (char c) -> c == 'y';
• (x, y) -> x + y;
• (int a, int b) -> a * a + b * b;
Functional Interfaces
• How does lambda expressions fit into Javas
type system?
• Each lambda corresponds to a given type,
specified by an interface
• exactly one abstract method declaration
• Interface with single abstract method used as
type
• Multiple optional default methods
Define a functional interface
@FunctionalInterface
public interface Calculator {
abstract int calculate(int x,int y);
}
public class FPDemo {
public static void main(String[] args) {
Calculator f=(x,y)->(x+y);
int z = f.calculate(3, 4);
System.out.println(z);
test((p,q)->p*q);
}
public static int test(Calculator cal) {
Return cal.calculate(4, 8);
}
Lambda Scopes
• int k=0;
• Calculator c1=
• (int x, int y)->
• {System.out.println(k);return x+y;};
• k=8;//fail to compile
• K is implicitly final
• Final is optional
Important functional interfaces in Java
• public interface Predicate<T> {
boolean test(T t);
}
• public interface Function<T,R> {
R apply(T t);
}
• public interface BinaryOperator<T> {
T apply(T left, T right);
}
public interface Consumer<T> {
void accept(T t);
}
• public interface Supplier<T> {
T get();
}
Predicate
• package com.java8.general;
• import java.util.Objects;
• import java.util.function.Predicate;
• public class PredicateTest {
• public static void main(String[] args) {
• Predicate<String> predicate = (s) -> s.length() > 0;
• boolean s=predicate.test("foo"); // true
• predicate.negate().test("foo");
• Predicate<Boolean> nonNull = Objects::nonNull;
• Predicate<Boolean> isNull = Objects::isNull;
• Predicate<String> isEmpty = String::isEmpty;
• Predicate<String> isNotEmpty = isEmpty.negate();
• }
• }
Functions
• Functions accept one argument and produce a
result.
• Function<String, Integer> toInteger =
Integer::valueOf;
• Function<String, Integer> toInteger=(s-
>Integer.valueOf(s);
Suppliers
• Suppliers produce a result of a given generic type.
Unlike Functions, Suppliers don't accept arguments.
• public class SupplierTest {
• public static void main(String[] args) {
• Supplier<SupplierTest> personSupplier =
SupplierTest::new;
• personSupplier.get(); // new Person
• }
• }
Consumers
• consumers represents operations to be
performed on a single input argument.
• Consumer<Person> greeter = (p) ->
System.out.println("Hello, " + p.firstName);
• greeter.accept(new Person("Luke",
"Skywalker"));
Comparators
• Comparator<Person> comparator = (p1, p2) ->
p1.firstName.compareTo(p2.firstName);
Person p1 = new Person("John", "Doe");
Person p2 = new Person("Alice",
"Wonderland");
• comparator.compare(p1, p2); // > 0
Type inference
• Predicate<Integer> atleast5=x->x>5;
• BinaryOperator<Long> addlngs=(x,y)->x+y;
• BinaryOperator add=(x,y)->x+y;
Streams
• A stream represents a sequence of elements
and supports different kind of operations to
perform computations upon those elements:
• List<String> myList =
• Arrays.asList("a1", "a2", "b1", "c2", "c1");
• myList.stream().filter(s -> s.startsWith("c"))
• .map(String::toUpperCase) .sorted()
• .forEach(System.out::println);
Traditional external iteration
• Int count=0;
• Iterator<Artist> iterator=allartists.iterator()
• While(iterator.hasNext())
• {Artist artist=iterator.next();
• If(artist.isForm(ā€œNYā€)
• Count++
• }
• Lot of boilerplate code and difficult concurrency
• Serial drawback
Internal Iterator with streams
• Long count=allartists.stream().filter(artist-
>artist.isFrom(ā€œNYā€)).count();
• Stream is tool for building up complex operations
on collections using functional approach
• If return is a stream its lazy-Intermediete stream
• If returns a value or void then its eager-Terminal
value
Terminal vs Lazy operations
• //does nothing
• artlist.stream().filter(artist->artist.isFrom("India"));
• //does nothing lazy inilitation
• artlist.stream().filter(artist-
>{System.out.println(artist.getName());
• return artist.isFrom("India");});
• long x=artlist.stream().filter(artist-
>{System.out.println(artist.getName());
• return artist.isFrom("India");}).count();
• System.out.println("x is"+x);
Common stream operations
• Collect(toList())
• Eager operation that genertes a list from the
values in a stream
• List<String>
collected=Stream.of("A","b","c").collect(Collec
tors.toList());
• Streams are lazy so u need eager operation
like collect
map
• Traditional uppercase conversion
• List<String> collected=new ArrayList<>();
• For(String string:asList(ā€œaā€,ā€bā€,ā€cā€)){
• String upper=string.toUpperCase();
• Collected.add(upper);
• }
• List<String>
mapped=Stream.of("A","b","c").map(string-
>string.toUpperCase()).collect(Collectors.toList());
filter
• Assume search strings start with a digit
• Traditional style-For loop and iterate
• Functional style
• List<String>
begwithn=Stream.of(ā€œaā€,ā€1abcā€,ā€abc1ā€).filter(v
alue->isDigit(value.charAt(0))).collect(toList());
• Predicate interface returns true/false
sorted
• stringCollection .stream() .sorted() .filter((s) ->
s.startsWith("a"))
.forEach(System.out::println);
• Sorted is an intermediate operation which
returns a sorted view of the stream. The
elements are sorted in natural order unless
you pass a custom Comparator.
Map and Match
• stringCollection .stream()
.map(String::toUpperCase) .sorted((a, b) ->
b.compareTo(a)) .forEach(System.out::println);
• boolean anyStartsWithA = stringCollection
.stream() .anyMatch((s) -> s.startsWith("a"));
flatmap
• Replace a value with a stream and
concantenate all streams together
• List<Integer>
together=Stream.of(Arrays.asList(1,2),Arrays.a
sList(3,4)).flatMap(numbers-
>numbers.stream()).collect(toList());
• Flatmap return type is a stream
Max and min
• List<Track> tracks=Arrays.asList(new
Track("track1",524),new
Track("track2",454),new
Track("track3",444));
• Track
shortesttrack=tracks.stream().min(Comparator
.comparing(track->track.getLength())).get();
• Comparing builds a comparator using keys
Reduction Operations
• Terminal operations ( average, sum, min, max,
and count) that return one value by combining
the contents of a stream
• reduction operations that return a collection
instead of a single value.
• general-purpose reduction operations reduce
and collect
Reduce
Optional<T> reduce(BinaryOperator<T> accumulator)Performs a reduction on the elements of this stream, using
an associative accumulation function, and returns an Optional describing the reduced value, if any.
T reduce(T identity, BinaryOperator<T> accumulator)Performs a reduction on the elements of this stream, using
the provided identity value and an associative accumulation function, and returns the reduced value.
<U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)Performs a reduction on
the elements of this stream, using the provided identity, accumulation and combining functions.
Reduce with BinaryOperator
• persons
• .stream()
• .reduce((p1, p2) -> p1.age > p2.age ? p1 :
p2)
• .ifPresent(System.out::println);
• // Pamela
• Returns Optional
Reduce with identity and accumilator
• Integer totalAgeReduce = roster
• .stream()
• .map(Person::getAge)
• .reduce(
• 0,
• (a, b) -> a + b);
• identity: The identity element is both the initial value of the reduction and
the default result if there are no elements in the stream
• accumulator: The accumulator function takes two parameters: a partial
result of the reduction (in this example, the sum of all processed integers
so far) and the next element of the stream (in this example, an integer).
(a, b) -> a + b
Generic reduce
• a reduce operation on elements of type <T> yielding a
result of type <U>
• <U> U reduce(U identity, BiFunction<U, ? super T, U>
accumulator, BinaryOperator<U> combiner);
• identity element is both an initial seed value for the
reduction and a default result if there are no input
elements
• The accumulator function takes a partial result and the
next element, and produces a new partial result
• The combiner function combines two partial results to
produce a new partial result.
• List<String> test= new ArrayList<String>();
• test.add("isuru");
• test.add("sam");
• test.add("silva");
• int s = test.stream().reduce(0, (x, y) -> x + y.length(), (x, y) -> x + y);
• - identity - identity value for the combiner function
- reducer - function for combining two results
- combiner - function for adding an additional element into a result.
• When you run the stream in parallel, the task is spanned into
multiple threads. So for example the data in the pipeline is
partitioned into chunks that evaluate and produce a result
independently. Then the combiner is used to merge this results.
Putting all together
• Get all artists for album
• Figure out which artists are bands
• Find the nationalities for each band
• Put together a set of these values
• Set<String>
origins=album.getMusicians().filter(artist-
>artist.getName().startsWith(ā€œTheā€)).map(arti
st->artist.getNationality()).collect(toSet());
• Filter,map are lazy operations
• Collect is eager
Stream misuse
• List<Artist>
musicians=album.getMusicians().collect(toList());
• List<Artist> bands=musicians.stream().filter(artist-
>artist.getName().startsWith(ā€œTheā€)).collect(toList());
• Set<String> origins=bands.stream.map(artist-
>artist.getNationality().collect(toSet());
• Its harder to read /boiler plate code
• Less efficient because it requires eagerly creating new
collection objects in each intermediate step
• Clutters the code with intermediate variables
• Multithreading/parrelllism issues
• Chain them
Overloading
• private interface IntegerBiFunction extends
BinaryOperator<Integer>{}
• public void overloadedm1(BinaryOperator<Integer>
lambda)
• {
• System.out.println("Binaryoperator");
• }
• public void overloadedm1(IntegerBiFunction lambda)
• {
• System.out.println("Bifunction");
• }
• public void test()
• {
• overloadedm1( (y,x)->x+1);
• }
• If there are several possible target types the
most specific type is inferred
• private interface IntPredicate
• {
• public boolean test(int value);
• }
• public void overloadp1(Predicate<Integer> predicate)
• {
• System.out.println("Predicate");
• }
• public void overloadp1(IntPredicate predicate)
• {
• System.out.println("Intpredicate");
• }
• If there are several possible target types and there is no specific type you have to
manually provid the type
Binary interface compatibility
• Backward binary compatibility-If you compile
app in Java1 to 7 it will run out of the box in
Java8
• Stream method added to java8 Collection
iface
• Breaks the binary compatibility
• Not compile or Exception by Calssloader
Advanced Collections and Collectors
• Method references
• Artist:getName
• Artist:new
• String[] new
Element Ordering
• List<Integer> numbers=Arrays.asList(2,3,4);
• List<Integer>
sameOrder=numbers.stream().collect(Collectors.t
oList());
• Set<Integer> numberset=new
HashSet<>(Arrays.asList(3,4,5,6));
• List<Integer>
ntsameOrderset=numberset.stream().collect(Coll
ectors.toList());
Collector
• toList ,toSet you done give the complete
implementation
• Colelcting values into a collection of specific
type
• Stream.collec(toCollection(TreeSet ::new))
To Values
• Collect into a single value using collector
• Finding the band with most numbers
• public Optional<Artist>
biggestGroup(Stream<Artist> artists)
• {
• Funtion<Artist,Long> getCount=artist-
>artist.getMembers().count();
• Return
artists.collect(maxBy(comparing(getCount)));
• minBy also there
Partioning the data
• Split out a list
• Public Map<Boolean,List<Artist>>
bandsAndsolo(Stream<Artist> artists)
• {
• return artists.collect(partitionBy(artist-
>artist.isSolo()));
• }
• Or partitionBy(Artist::isSolo) method reference
Grouping the data
• Grouping albums by main artist
• Public Map<Artist,List<Album>>
albumsByArtists(Stream<Album> albums)
• {
• Return albums.collect(groupingBy(album-
>album.getMainMusician()));
• }
String
• Traditional String handling
• StringBuilder builder=new StringBuilder(ā€œ[ā€œ);
• For(Artist artist:artists)
• {
• If(builder.length()>1)
• Builder.apppend(ā€œ, ā€œ);
• }
• String name=artist.getName();
• Builder.append(name);
• }
• Builder.append(ā€œ]ā€);
• String result=builder.toString();
• String result=artists.stream().map(Artist::getName).collect(Collectors.joiniing(ā€œ, ā€œ,
ā€œ[ā€œ,ā€]ā€));
Composing collectors
• NaĆÆve approach
• Map<Artist,List<Album>>
albumByArtist=albums.collect(groupingBy(album-
>album.getMainMusician())));
• Map<Artist,Integer> numberofalbums=new
HashMap<>();
• For(Entry<Artist,List<Album>
entry:albumsByArtist.entrySet()){
• Numberofalbums.put(entry.getKey(),entry.getVal
ue().size());
Using collectors to count the number
of albums for each artist
• Public Map<Artist,Long>
numberOfalbums(Stream<Album> albums)
• {Return albums.collect(groupingBy(album-
>album.getMusician(),counting())));
• }
• This grouping devides elements into buckets
• Reduction as a collector
Data paralleism
• Parellism vs Concurrency
• Concurrency arises when 2 tasks are making
progress at overlapping time periods
• Parrellism arises 2 tasks are hapenning at
same time –multicore cpu
• In single cpu-concurrency
• Multi core-concurrent/parrellel
Data parellism..contd
• Splitting up the data to be operated on and
assigning single processing unit to each chunk
of data
• Perform same operation on a large dataset
• Task parellism-each individual thread of
execution can be doing totally different task
• Java EE container TP
Parellel stream operations
• Serial summing of album trak lengths
• Public int serialArraySum(){
• Return
album.stream().flatmap(Album::gettracks).mapToInt(tr
ack:getLength).sum();
• }
• Public int parreelelArraySum(){
• Return
albums.parallelstream().flatMap(Album::gettracks).ma
pToInt(Track::getLength).sum();
• When 10000 albums are hit parrel code is faster
Testing Lambdas
• Public static List<String>
allToUppercase(List<String> words)
• {
• Return words.stream().map(string-
>string.toUpperCase()).collect(Collectors.<Stri
ng>.toList());
• }
• @Test
• Public void multiplewordsToUpperCase()
• {
• List<String> input=Arrays.asList(ā€œaā€,ā€bā€,ā€helloā€);
• List<String> result=Testing.allToUppercase(input);
• assertEquals(asList(ā€œAā€,ā€Bā€,ā€HELLOā€),result);
• }
Ad

More Related Content

What's hot (20)

Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
Ā 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
Venkata Naga Ravi
Ā 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
Tobias Coetzee
Ā 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
Martin Toshev
Ā 
Julie Michelman - Pandas, Pipelines, and Custom Transformers
Julie Michelman - Pandas, Pipelines, and Custom TransformersJulie Michelman - Pandas, Pipelines, and Custom Transformers
Julie Michelman - Pandas, Pipelines, and Custom Transformers
PyData
Ā 
Project Lambda: Evolution of Java
Project Lambda: Evolution of JavaProject Lambda: Evolution of Java
Project Lambda: Evolution of Java
Can Pekdemir
Ā 
Evaluating Hype in scala
Evaluating Hype in scalaEvaluating Hype in scala
Evaluating Hype in scala
Pere Villega
Ā 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
Ā 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
RatnaJava
Ā 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
Knoldus Inc.
Ā 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
Ā 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
Ā 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
Ā 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
Michiel Borkent
Ā 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
Jim Bethancourt
Ā 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
langer4711
Ā 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
Dev Raj Gautam
Ā 
Java 8 ​and ​Best Practices
Java 8 ​and ​Best PracticesJava 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
Buddhini Seneviratne
Ā 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
Garth Gilmour
Ā 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
Ā 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
Ā 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
Venkata Naga Ravi
Ā 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
Tobias Coetzee
Ā 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
Martin Toshev
Ā 
Julie Michelman - Pandas, Pipelines, and Custom Transformers
Julie Michelman - Pandas, Pipelines, and Custom TransformersJulie Michelman - Pandas, Pipelines, and Custom Transformers
Julie Michelman - Pandas, Pipelines, and Custom Transformers
PyData
Ā 
Project Lambda: Evolution of Java
Project Lambda: Evolution of JavaProject Lambda: Evolution of Java
Project Lambda: Evolution of Java
Can Pekdemir
Ā 
Evaluating Hype in scala
Evaluating Hype in scalaEvaluating Hype in scala
Evaluating Hype in scala
Pere Villega
Ā 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
Ā 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
RatnaJava
Ā 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
Knoldus Inc.
Ā 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
Ā 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
Ā 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
Ā 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
Michiel Borkent
Ā 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
Jim Bethancourt
Ā 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
langer4711
Ā 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
Dev Raj Gautam
Ā 
Java 8 ​and ​Best Practices
Java 8 ​and ​Best PracticesJava 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
Buddhini Seneviratne
Ā 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
Garth Gilmour
Ā 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
Ā 

Viewers also liked (20)

Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
yousry ibrahim
Ā 
Java8 Lambda and Streams
Java8 Lambda and StreamsJava8 Lambda and Streams
Java8 Lambda and Streams
Mindfire Solutions
Ā 
Java8
Java8Java8
Java8
Sunil Kumar
Ā 
Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8 Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8
xshyamx
Ā 
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
Sergii Maliarov
Ā 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
Ā 
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfacesJava8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Dirk Detering
Ā 
2014 java functional
2014 java functional2014 java functional
2014 java functional
Demian Neidetcher
Ā 
Java8
Java8Java8
Java8
Jadson Santos
Ā 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
Ā 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Ruslan Shevchenko
Ā 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
Yiguang Hu
Ā 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
Andrea Iacono
Ā 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
Marut Singh
Ā 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
Marut Singh
Ā 
Java8 training - class 3
Java8 training - class 3Java8 training - class 3
Java8 training - class 3
Marut Singh
Ā 
Java8 - Under the hood
Java8 - Under the hoodJava8 - Under the hood
Java8 - Under the hood
Lakshmi Narasimhan
Ā 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
Peter Lawrey
Ā 
A Quick peek @ New Date & Time API of Java 8
A Quick peek @ New Date & Time API of Java 8A Quick peek @ New Date & Time API of Java 8
A Quick peek @ New Date & Time API of Java 8
Buddha Jyothiprasad
Ā 
Java8
Java8Java8
Java8
Freeman Zhang
Ā 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
yousry ibrahim
Ā 
Java8 Lambda and Streams
Java8 Lambda and StreamsJava8 Lambda and Streams
Java8 Lambda and Streams
Mindfire Solutions
Ā 
Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8 Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8
xshyamx
Ā 
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
Sergii Maliarov
Ā 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
Ā 
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfacesJava8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Dirk Detering
Ā 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
Ā 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Ruslan Shevchenko
Ā 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
Yiguang Hu
Ā 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
Andrea Iacono
Ā 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
Marut Singh
Ā 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
Marut Singh
Ā 
Java8 training - class 3
Java8 training - class 3Java8 training - class 3
Java8 training - class 3
Marut Singh
Ā 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
Peter Lawrey
Ā 
A Quick peek @ New Date & Time API of Java 8
A Quick peek @ New Date & Time API of Java 8A Quick peek @ New Date & Time API of Java 8
A Quick peek @ New Date & Time API of Java 8
Buddha Jyothiprasad
Ā 
Ad

Similar to Exploring Streams and Lambdas in Java8 (20)

Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
Aniket Thakur
Ā 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
Ā 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
Ā 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
Ā 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
Ā 
RQP reverse query processing it's application 2011.pptx
RQP reverse query processing it's application 2011.pptxRQP reverse query processing it's application 2011.pptx
RQP reverse query processing it's application 2011.pptx
ajajkhan16
Ā 
Java data types
Java data typesJava data types
Java data types
Kuppusamy P
Ā 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
khush_boo31
Ā 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
Ortus Solutions, Corp
Ā 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
Ortus Solutions, Corp
Ā 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
franciscoortin
Ā 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
Ā 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen Borgers
NLJUG
Ā 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
Ā 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
Anna Gerber
Ā 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
Ā 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
Ā 
ITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
ITB2019 RuleBox : The natural rule engine for CFML - Luis MajanoITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
ITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
Ortus Solutions, Corp
Ā 
RuleBox : A natural language Rule Engine
RuleBox : A natural language Rule EngineRuleBox : A natural language Rule Engine
RuleBox : A natural language Rule Engine
Ortus Solutions, Corp
Ā 
Unit-3.pptx.pdf java api knowledge apiii
Unit-3.pptx.pdf java api knowledge apiiiUnit-3.pptx.pdf java api knowledge apiii
Unit-3.pptx.pdf java api knowledge apiii
mpfbaa
Ā 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
Aniket Thakur
Ā 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
Ā 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
Ā 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
Ā 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
Ā 
RQP reverse query processing it's application 2011.pptx
RQP reverse query processing it's application 2011.pptxRQP reverse query processing it's application 2011.pptx
RQP reverse query processing it's application 2011.pptx
ajajkhan16
Ā 
Java data types
Java data typesJava data types
Java data types
Kuppusamy P
Ā 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
khush_boo31
Ā 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
Ortus Solutions, Corp
Ā 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
Ortus Solutions, Corp
Ā 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
franciscoortin
Ā 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
Ā 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen Borgers
NLJUG
Ā 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
Anna Gerber
Ā 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
Ā 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
Ā 
ITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
ITB2019 RuleBox : The natural rule engine for CFML - Luis MajanoITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
ITB2019 RuleBox : The natural rule engine for CFML - Luis Majano
Ortus Solutions, Corp
Ā 
RuleBox : A natural language Rule Engine
RuleBox : A natural language Rule EngineRuleBox : A natural language Rule Engine
RuleBox : A natural language Rule Engine
Ortus Solutions, Corp
Ā 
Unit-3.pptx.pdf java api knowledge apiii
Unit-3.pptx.pdf java api knowledge apiiiUnit-3.pptx.pdf java api knowledge apiii
Unit-3.pptx.pdf java api knowledge apiii
mpfbaa
Ā 
Ad

Recently uploaded (20)

Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
Ā 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
Ā 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
Ā 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
Ā 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
Ā 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
Ā 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
Ā 
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdfKit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Wonjun Hwang
Ā 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
Ā 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
Ā 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
Ā 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
Ā 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
Ā 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
Ā 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
Ā 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
Ā 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
Ā 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
Ā 
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜øKit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Wonjun Hwang
Ā 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
Ā 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
Ā 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
Ā 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
Ā 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
Ā 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
Ā 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
Ā 
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdfKit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Kit-Works Team Study_ķŒ€ģŠ¤ķ„°ė””_ź¹€ķ•œģ†”_nuqs_20250509.pdf
Wonjun Hwang
Ā 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
Ā 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
Ā 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
Ā 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
Ā 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
Ā 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
Ā 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
Ā 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
Ā 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
Ā 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
Ā 
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜øKit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Kit-Works Team Study_ģ•„ģ§ė„ Dockefile.pdf_ź¹€ģ„±ķ˜ø
Wonjun Hwang
Ā 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
Ā 

Exploring Streams and Lambdas in Java8

  • 1. Exploring Streams and Lambdas in Java8 • Presented By:Isuru Samaraweera
  • 2. Agenda • Why change Java again? • What is FP & Lambda? • Functional Interfaces • Streams • Reduction • Overloading Lambdas • Advanced Collections and collectors • Partioning and Grouping data • Data Parrellelism • Testing Lambdas
  • 3. Why change java again • Rise of the multicore CPUS • Algorithms involves locks error-prone time consuming • Util.concurrent libraries have limititaions • Lack of efficient parrelel operations on a collection • Java8 allows complex collection-processing algorithms
  • 4. What is fp • Oop data abstraction/side efects • Functional focuses on side effect free • Pure functions/lambdas • Pass functions around easeir to write lazy code which initialises values when necessary • n -> n % 2 != 0; • (char c) -> c == 'y'; • (x, y) -> x + y; • (int a, int b) -> a * a + b * b;
  • 5. Functional Interfaces • How does lambda expressions fit into Javas type system? • Each lambda corresponds to a given type, specified by an interface • exactly one abstract method declaration • Interface with single abstract method used as type • Multiple optional default methods
  • 6. Define a functional interface @FunctionalInterface public interface Calculator { abstract int calculate(int x,int y); } public class FPDemo { public static void main(String[] args) { Calculator f=(x,y)->(x+y); int z = f.calculate(3, 4); System.out.println(z); test((p,q)->p*q); } public static int test(Calculator cal) { Return cal.calculate(4, 8); }
  • 7. Lambda Scopes • int k=0; • Calculator c1= • (int x, int y)-> • {System.out.println(k);return x+y;}; • k=8;//fail to compile • K is implicitly final • Final is optional
  • 8. Important functional interfaces in Java • public interface Predicate<T> { boolean test(T t); } • public interface Function<T,R> { R apply(T t); } • public interface BinaryOperator<T> { T apply(T left, T right); } public interface Consumer<T> { void accept(T t); } • public interface Supplier<T> { T get(); }
  • 9. Predicate • package com.java8.general; • import java.util.Objects; • import java.util.function.Predicate; • public class PredicateTest { • public static void main(String[] args) { • Predicate<String> predicate = (s) -> s.length() > 0; • boolean s=predicate.test("foo"); // true • predicate.negate().test("foo"); • Predicate<Boolean> nonNull = Objects::nonNull; • Predicate<Boolean> isNull = Objects::isNull; • Predicate<String> isEmpty = String::isEmpty; • Predicate<String> isNotEmpty = isEmpty.negate(); • } • }
  • 10. Functions • Functions accept one argument and produce a result. • Function<String, Integer> toInteger = Integer::valueOf; • Function<String, Integer> toInteger=(s- >Integer.valueOf(s);
  • 11. Suppliers • Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept arguments. • public class SupplierTest { • public static void main(String[] args) { • Supplier<SupplierTest> personSupplier = SupplierTest::new; • personSupplier.get(); // new Person • } • }
  • 12. Consumers • consumers represents operations to be performed on a single input argument. • Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); • greeter.accept(new Person("Luke", "Skywalker"));
  • 13. Comparators • Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Alice", "Wonderland"); • comparator.compare(p1, p2); // > 0
  • 14. Type inference • Predicate<Integer> atleast5=x->x>5; • BinaryOperator<Long> addlngs=(x,y)->x+y; • BinaryOperator add=(x,y)->x+y;
  • 15. Streams • A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements: • List<String> myList = • Arrays.asList("a1", "a2", "b1", "c2", "c1"); • myList.stream().filter(s -> s.startsWith("c")) • .map(String::toUpperCase) .sorted() • .forEach(System.out::println);
  • 16. Traditional external iteration • Int count=0; • Iterator<Artist> iterator=allartists.iterator() • While(iterator.hasNext()) • {Artist artist=iterator.next(); • If(artist.isForm(ā€œNYā€) • Count++ • } • Lot of boilerplate code and difficult concurrency • Serial drawback
  • 17. Internal Iterator with streams • Long count=allartists.stream().filter(artist- >artist.isFrom(ā€œNYā€)).count(); • Stream is tool for building up complex operations on collections using functional approach • If return is a stream its lazy-Intermediete stream • If returns a value or void then its eager-Terminal value
  • 18. Terminal vs Lazy operations • //does nothing • artlist.stream().filter(artist->artist.isFrom("India")); • //does nothing lazy inilitation • artlist.stream().filter(artist- >{System.out.println(artist.getName()); • return artist.isFrom("India");}); • long x=artlist.stream().filter(artist- >{System.out.println(artist.getName()); • return artist.isFrom("India");}).count(); • System.out.println("x is"+x);
  • 19. Common stream operations • Collect(toList()) • Eager operation that genertes a list from the values in a stream • List<String> collected=Stream.of("A","b","c").collect(Collec tors.toList()); • Streams are lazy so u need eager operation like collect
  • 20. map • Traditional uppercase conversion • List<String> collected=new ArrayList<>(); • For(String string:asList(ā€œaā€,ā€bā€,ā€cā€)){ • String upper=string.toUpperCase(); • Collected.add(upper); • } • List<String> mapped=Stream.of("A","b","c").map(string- >string.toUpperCase()).collect(Collectors.toList());
  • 21. filter • Assume search strings start with a digit • Traditional style-For loop and iterate • Functional style • List<String> begwithn=Stream.of(ā€œaā€,ā€1abcā€,ā€abc1ā€).filter(v alue->isDigit(value.charAt(0))).collect(toList()); • Predicate interface returns true/false
  • 22. sorted • stringCollection .stream() .sorted() .filter((s) -> s.startsWith("a")) .forEach(System.out::println); • Sorted is an intermediate operation which returns a sorted view of the stream. The elements are sorted in natural order unless you pass a custom Comparator.
  • 23. Map and Match • stringCollection .stream() .map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a)) .forEach(System.out::println); • boolean anyStartsWithA = stringCollection .stream() .anyMatch((s) -> s.startsWith("a"));
  • 24. flatmap • Replace a value with a stream and concantenate all streams together • List<Integer> together=Stream.of(Arrays.asList(1,2),Arrays.a sList(3,4)).flatMap(numbers- >numbers.stream()).collect(toList()); • Flatmap return type is a stream
  • 25. Max and min • List<Track> tracks=Arrays.asList(new Track("track1",524),new Track("track2",454),new Track("track3",444)); • Track shortesttrack=tracks.stream().min(Comparator .comparing(track->track.getLength())).get(); • Comparing builds a comparator using keys
  • 26. Reduction Operations • Terminal operations ( average, sum, min, max, and count) that return one value by combining the contents of a stream • reduction operations that return a collection instead of a single value. • general-purpose reduction operations reduce and collect
  • 27. Reduce Optional<T> reduce(BinaryOperator<T> accumulator)Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. T reduce(T identity, BinaryOperator<T> accumulator)Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
  • 28. Reduce with BinaryOperator • persons • .stream() • .reduce((p1, p2) -> p1.age > p2.age ? p1 : p2) • .ifPresent(System.out::println); • // Pamela • Returns Optional
  • 29. Reduce with identity and accumilator • Integer totalAgeReduce = roster • .stream() • .map(Person::getAge) • .reduce( • 0, • (a, b) -> a + b); • identity: The identity element is both the initial value of the reduction and the default result if there are no elements in the stream • accumulator: The accumulator function takes two parameters: a partial result of the reduction (in this example, the sum of all processed integers so far) and the next element of the stream (in this example, an integer). (a, b) -> a + b
  • 30. Generic reduce • a reduce operation on elements of type <T> yielding a result of type <U> • <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner); • identity element is both an initial seed value for the reduction and a default result if there are no input elements • The accumulator function takes a partial result and the next element, and produces a new partial result • The combiner function combines two partial results to produce a new partial result.
  • 31. • List<String> test= new ArrayList<String>(); • test.add("isuru"); • test.add("sam"); • test.add("silva"); • int s = test.stream().reduce(0, (x, y) -> x + y.length(), (x, y) -> x + y); • - identity - identity value for the combiner function - reducer - function for combining two results - combiner - function for adding an additional element into a result. • When you run the stream in parallel, the task is spanned into multiple threads. So for example the data in the pipeline is partitioned into chunks that evaluate and produce a result independently. Then the combiner is used to merge this results.
  • 32. Putting all together • Get all artists for album • Figure out which artists are bands • Find the nationalities for each band • Put together a set of these values
  • 34. Stream misuse • List<Artist> musicians=album.getMusicians().collect(toList()); • List<Artist> bands=musicians.stream().filter(artist- >artist.getName().startsWith(ā€œTheā€)).collect(toList()); • Set<String> origins=bands.stream.map(artist- >artist.getNationality().collect(toSet()); • Its harder to read /boiler plate code • Less efficient because it requires eagerly creating new collection objects in each intermediate step • Clutters the code with intermediate variables • Multithreading/parrelllism issues • Chain them
  • 35. Overloading • private interface IntegerBiFunction extends BinaryOperator<Integer>{} • public void overloadedm1(BinaryOperator<Integer> lambda) • { • System.out.println("Binaryoperator"); • } • public void overloadedm1(IntegerBiFunction lambda) • { • System.out.println("Bifunction"); • }
  • 36. • public void test() • { • overloadedm1( (y,x)->x+1); • } • If there are several possible target types the most specific type is inferred
  • 37. • private interface IntPredicate • { • public boolean test(int value); • } • public void overloadp1(Predicate<Integer> predicate) • { • System.out.println("Predicate"); • } • public void overloadp1(IntPredicate predicate) • { • System.out.println("Intpredicate"); • } • If there are several possible target types and there is no specific type you have to manually provid the type
  • 38. Binary interface compatibility • Backward binary compatibility-If you compile app in Java1 to 7 it will run out of the box in Java8 • Stream method added to java8 Collection iface • Breaks the binary compatibility • Not compile or Exception by Calssloader
  • 39. Advanced Collections and Collectors • Method references • Artist:getName • Artist:new • String[] new
  • 40. Element Ordering • List<Integer> numbers=Arrays.asList(2,3,4); • List<Integer> sameOrder=numbers.stream().collect(Collectors.t oList()); • Set<Integer> numberset=new HashSet<>(Arrays.asList(3,4,5,6)); • List<Integer> ntsameOrderset=numberset.stream().collect(Coll ectors.toList());
  • 41. Collector • toList ,toSet you done give the complete implementation • Colelcting values into a collection of specific type • Stream.collec(toCollection(TreeSet ::new))
  • 42. To Values • Collect into a single value using collector • Finding the band with most numbers • public Optional<Artist> biggestGroup(Stream<Artist> artists) • { • Funtion<Artist,Long> getCount=artist- >artist.getMembers().count(); • Return artists.collect(maxBy(comparing(getCount))); • minBy also there
  • 43. Partioning the data • Split out a list • Public Map<Boolean,List<Artist>> bandsAndsolo(Stream<Artist> artists) • { • return artists.collect(partitionBy(artist- >artist.isSolo())); • } • Or partitionBy(Artist::isSolo) method reference
  • 44. Grouping the data • Grouping albums by main artist • Public Map<Artist,List<Album>> albumsByArtists(Stream<Album> albums) • { • Return albums.collect(groupingBy(album- >album.getMainMusician())); • }
  • 45. String • Traditional String handling • StringBuilder builder=new StringBuilder(ā€œ[ā€œ); • For(Artist artist:artists) • { • If(builder.length()>1) • Builder.apppend(ā€œ, ā€œ); • } • String name=artist.getName(); • Builder.append(name); • } • Builder.append(ā€œ]ā€); • String result=builder.toString(); • String result=artists.stream().map(Artist::getName).collect(Collectors.joiniing(ā€œ, ā€œ, ā€œ[ā€œ,ā€]ā€));
  • 46. Composing collectors • NaĆÆve approach • Map<Artist,List<Album>> albumByArtist=albums.collect(groupingBy(album- >album.getMainMusician()))); • Map<Artist,Integer> numberofalbums=new HashMap<>(); • For(Entry<Artist,List<Album> entry:albumsByArtist.entrySet()){ • Numberofalbums.put(entry.getKey(),entry.getVal ue().size());
  • 47. Using collectors to count the number of albums for each artist • Public Map<Artist,Long> numberOfalbums(Stream<Album> albums) • {Return albums.collect(groupingBy(album- >album.getMusician(),counting()))); • } • This grouping devides elements into buckets • Reduction as a collector
  • 48. Data paralleism • Parellism vs Concurrency • Concurrency arises when 2 tasks are making progress at overlapping time periods • Parrellism arises 2 tasks are hapenning at same time –multicore cpu • In single cpu-concurrency • Multi core-concurrent/parrellel
  • 49. Data parellism..contd • Splitting up the data to be operated on and assigning single processing unit to each chunk of data • Perform same operation on a large dataset • Task parellism-each individual thread of execution can be doing totally different task • Java EE container TP
  • 50. Parellel stream operations • Serial summing of album trak lengths • Public int serialArraySum(){ • Return album.stream().flatmap(Album::gettracks).mapToInt(tr ack:getLength).sum(); • } • Public int parreelelArraySum(){ • Return albums.parallelstream().flatMap(Album::gettracks).ma pToInt(Track::getLength).sum(); • When 10000 albums are hit parrel code is faster
  • 51. Testing Lambdas • Public static List<String> allToUppercase(List<String> words) • { • Return words.stream().map(string- >string.toUpperCase()).collect(Collectors.<Stri ng>.toList()); • }
  • 52. • @Test • Public void multiplewordsToUpperCase() • { • List<String> input=Arrays.asList(ā€œaā€,ā€bā€,ā€helloā€); • List<String> result=Testing.allToUppercase(input); • assertEquals(asList(ā€œAā€,ā€Bā€,ā€HELLOā€),result); • }
  ēæ»čÆ‘ļ¼š