principles of so3ware construc9on: objects, design, and ...charlie/courses/17-214/... · 4/12/2018...

45
1 17-214 School of Computer Science Principles of So3ware Construc9on: Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams Charlie Garrod Bogdan Vasilescu

Upload: others

Post on 14-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

1 17-214

SchoolofComputerScience

PrinciplesofSo3wareConstruc9on: Objects,Design,andConcurrencyPart5:EtceteraJavalambdasandstreamsCharlieGarrod BogdanVasilescu

Page 2: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

2 17-214

Administrivia

•  Homework5BestFrameworksavailabletoday•  Homework5cdueMonday11:59p.m

Page 3: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

3 17-214

KeyconceptsfromTuesday

Page 4: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

4 17-214

Producer-consumerdesignpaSern

•  Goal:Decoupletheproducerandtheconsumerofsomedata•  Consequences:

–  Removescodedependencybetweenproducersandconsumers–  Producersandconsumerscanproduceandconsumeatdifferentrates

Page 5: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

5 17-214

ThemembranepaSern

•  Mul9pleroundsoffork-join,eachroundwai9ngforthepreviousroundtocomplete

Image from: Wikipedia

Page 6: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

6 17-214

Parallelprefixsumsalgorithm,upsweep

•  Computesthepar9alsumsinamoreusefulmanner

[13, 9, -4, 19, -6, 2, 6, 3]

[13, 22, -4, 15, -6, -4, 6, 9]

[13, 22, -4, 37, -6, -4, 6, 5]

[13, 22, -4, 37, -6, -4, 6, 42]

Page 7: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

7 17-214

Parallelprefixsumsalgorithm,downsweep

•  Nowunwindstocalculatetheothersums

[13, 22, -4, 37, -6, -4, 6, 42]

[13, 22, -4, 37, -6, 33, 6, 42]

[13, 22, 18, 37, 31, 33, 39, 42]

•  Recall,westartedwith:[13, 9, -4, 19, -6, 2, 6, 3]

Page 8: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

8 17-214

Doublingarraysizeaddstwomorelevels

Upsweep

Downsweep

Page 9: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

9 17-214

Fork/Join:computa9onalpaSernforparallelism

•  Forkataskintosubtasks•  Jointhesubtasks(i.e.,waitforthemtocomplete)•  Subtasksaredecomposedrecursively

•  Thejava.util.concurrent.ForkJoinPoolclass–  ImplementsExecutorService–  Executes java.util.concurrent.ForkJoinTask<V>or

java.util.concurrent.RecursiveTask<V>or java.util.concurrent.RecursiveAction

•  Thethreadsinthefork-joinpooldoworkstealing

Page 10: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

10 17-214

AForkJoinexample

•  SeePrefixSumsParallelForkJoin.java•  Seetheprocessorgo,gogo!

Page 11: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

11 17-214

Parallelprefixsumsalgorithm

•  Howgoodisthis?–  Work:O(n),Depth:O(lgn)

•  Compareto:PrefixSumsParallelArrays.java

Page 12: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

12 17-214

Parallelprefixsumsalgorithm

•  Howgoodisthis?–  Work:O(n),Depth:O(lgn)

•  Compareto:PrefixSumsParallelArrays.java•  Comparetothesequen9alalgorithm:

–  SeePrefixSumsSequential.java

Page 13: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

13 17-214

Parallelprefixsumsalgorithm

•  Howgoodisthis?–  Work:O(n),Depth:O(lgn)

•  Compareto:PrefixSumsParallelArrays.java•  Comparetothesequen9alalgorithm:

–  SeePrefixSumsSequential.java–  n-1addi9ons–  Memoryaccessissequen9al

•  Theparallelalgorithm:–  About2nusefuladdi9ons,plusextraaddi9onsfortheloopindexes–  Memoryaccessisnon-sequen9al

•  Thepunchline:–  Don'trollyourown–  CacheandconstantsmaSer–  Thebestparallelimplementa9onwasnobeSerthannaïvesequen9al

Page 14: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

14 17-214

Today

•  Javalambdasandfunc9onalinterfaces•  Javastreams

Page 15: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

15 17-214

Lambdas,briefly

•  Termcomesfromλ-Calculus–  Everythingisafunc9on!

•  Alambda(λ)isananonymousfunc9on

Page 16: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

16 17-214

DoesJavahavelambdas?

A.  Yes,it’shadthemsincethebeginningB.  Yes,it’shadthemsinceanonymousclasses(1.1)C.  Yes,it’shadthemsinceJava8—thespecsaysso!D.  No,neverhad’em,neverwill

Page 17: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

17 17-214

Func9onobjectsinJava1.0

classStringLengthComparatorimplementsComparator{privateStringLengthComparator(){}publicstaticfinalStringLengthComparatorINSTANCE=newStringLengthComparator();publicintcompare(Objecto1,Objecto2){Strings1=(String)o1,s2=(String)o2;returns1.length()-s2.length();}}Arrays.sort(words,StringLengthComparator.INSTANCE);

Page 18: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

18 17-214

Func9onobjectsinJava1.1

Arrays.sort(words,newComparator(){publicintcompare(Objecto1,Objecto2){Strings1=(String)o1,s2=(String)o2;returns1.length()-s2.length();}});

"ClassInstanceCrea9onExpression"(CICE)

Page 19: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

19 17-214

Func9onobjectsinJava5

Arrays.sort(words,newComparator<String>(){publicintcompare(Strings1,Strings2){returns1.length()-s2.length();}});

CICEwithgenerics

Page 20: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

20 17-214

Func9onobjectsinJava8

Arrays.sort(words,(s1,s2)->s1.length()-s2.length());

•  Theyfeellikelambdas,they’recalledlambdas

Page 21: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

21 17-214

Lambdasyntax

Syntax Exampleparameter->expression x->x*x

parameter->block s->{System.out.println(s);}

(parameters)->expression (x,y)->Math.sqrt(x*x+y*y)

(parameters)->block (s1,s2)->{System.out.println(s1+","+s2);}

(parameterdecls)->expression (doublex,doubley)->Math.sqrt(x*x+y*y)

(parametersdecls)->block (List<?>list)->{Arrays.shuffle(list);Arrays.sort(list);}

Page 22: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

22 17-214

Methodreferences:moresuccinctthanlambdas

•  Asta9cmethod–  e.g.,Math::cos

•  Anunboundinstancemethod(whosereceiverisunspecified)–  e.g.,String::length–  Theresul9ngfunc9onhasanextraargumentforthereceiver

•  Aboundinstancemethodofaspecificobject–  e.g.,System.out::println

•  Aconstructor–  e.g.,Integer::new,String[]::new

Page 23: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

23 17-214

Nofunc9ontypesinJava,onlyfunc3onalinterfaces

•  Interfaceswithonlyoneabstractmethod•  Op9onallyannotatedwith@FunctionalInterface•  Somefunc9onalinterfacesyouknow

–  java.lang.Runnable–  java.util.concurrent.Callable–  java.util.Comparator–  java.awt.event.ActionListener–  Many,manymoreinjava.util.function

Page 24: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

24 17-214

Func9oninterfacesinjava.util.function

BiConsumer<T,U>BiFunction<T,U,R>BinaryOperator<T>BiPredicate<T,U>BooleanSupplierConsumer<T>DoubleBinaryOperatorDoubleConsumerDoubleFunction<R>DoublePredicateDoubleSupplierDoubleToIntFunctionDoubleToLongFunctionDoubleUnaryOperatorFunction<T,R>IntBinaryOperatorIntConsumerIntFunction<R>IntPredicateIntSupplierIntToDoubleFunctionIntToLongFunction

IntUnaryOperatorLongBinaryOperatorLongConsumerLongFunction<R>LongPredicateLongSupplierLongToDoubleFunctionLongToIntFunctionLongUnaryOperatorObjDoubleConsumer<T>ObjIntConsumer<T>ObjLongConsumer<T>Predicate<T>Supplier<T>ToDoubleBiFunction<T,U>ToDoubleFunction<T>ToIntBiFunction<T,U>ToIntFunction<T>ToLongBiFunction<T,U>ToLongFunction<T>UnaryOperator<T>

Page 25: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

25 17-214

SomeFunction<String,Integer>

Descrip<on CodeLambda s->Integer.parseInt(s)

Lambdaw/explicitparamtype (Strings)->Integer.parseInt(s)

Sta9cmethodreference Integer::parseInt

Constructorreference Integer::new

Instancemethodreference String::length

AnonymousclassICE newFunction<String,Integer>(){publicIntegerapply(Strings){returns.length();}}

Page 26: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

26 17-214

Javastreams

•  Astreamisabunchofdataobjects,typicallyfromacollec9on,array,orinputdevice,forprocessing

•  Processedbyapipeline–  Asinglestreamgenerator(datasource)–  Zeroormoreintermediatestreamopera.ons–  Asingleterminalstreamopera.on

Page 27: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

27 17-214

Streamexamples:Itera9on

//IterationoveracollectionstaticList<String>stringList=...;stringList.stream().forEach(System.out::println);//IterationoverarangeofintegersIntStream.range(0,10).forEach(System.out::println);//Aminipuzzler:whatdoesthisprint?"Helloworld!".chars().forEach(System.out::print);

Page 28: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

28 17-214

Puzzlersolu9on

"Helloworld!".chars().forEach(System.out::print);

Prints"721011081081113211911111410810033"

ThecharsmethodonStringreturnsanIntStream

Page 29: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

29 17-214

Howdoyoufixit?

"Helloworld!".chars().forEach(x->System.out.print((char)x));

•  Nowprints"Helloworld!"

•  Morals:–  Streamsonlyforobjectreftypes,int,long,anddouble–  Typeinferencecanbeconfusing

Page 30: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

30 17-214

Streamexamples:mapping,filtering

List<String>longStrings=stringList.stream().filter(s->s.length()>42).collect(Collectors.toList());

List<String>firstLetters=stringList.stream().map(s->s.substring(0,1)).collect(Collectors.toList());

List<String>firstLetterOfLongStrings=stringList.stream().filter(s->s.length()>42).map(s->s.substring(0,1)).collect(Collectors.toList());

Page 31: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

31 17-214

Streamexamples:duplicates,sor9ng

List<String>dupsRemoved=stringList.stream().map(s->s.substring(0,1)).distinct().collect(Collectors.toList());List<String>sortedList=stringList.stream().map(s->s.substring(0,1)).sorted()//Bufferseverythinguntilterminalop.collect(Collectors.toList());

Page 32: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

32 17-214

Streamexamples:bulkpredicates

booleanallStringHaveLengthThree=stringList.stream().allMatch(s->s.length()==3);booleananyStringHasLengthThree=stringList.stream().anyMatch(s->s.length()==3);

Page 33: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

33 17-214

Streamsareprocessedlazily

•  Dataispulledbyterminalopera9on,notpushedbysource–  Infinitestreamsarenotaproblem

•  Intermediateopera9onscanbefused–  Mul9pleintermediateopera9onsusuallydon’tcausemul9pletraversals

•  Intermediateresultsusuallynotstored–  Butthereareexcep9ons(e.g.,sorted)

Page 34: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

34 17-214

Easyparallelism:.parallelStream()

List<String>longStrings=stringList.parallelStream().filter(s->s.length()>42).collect(Collectors.toList());

List<String>firstLetters=stringList.parallelStream().map(s->s.substring(0,1)).collect(Collectors.toList());

List<String>firstLetterOfLongStrings=stringList.parallelStream().filter(s->s.length()>42).map(s->s.substring(0,1)).collect(Collectors.toList());

Page 35: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

35 17-214

Streaminterfaceisamonster(1/3)

publicinterfaceStream<T>extendsBaseStream<T,Stream<T>>{//IntermediateOperationsStream<T>filter(Predicate<T>);<R>Stream<R>map(Function<T,R>);IntStreammapToInt(ToIntFunction<T>);LongStreammapToLong(ToLongFunction<T>);DoubleStreammapToDouble(ToDoubleFunction<T>);<R>Stream<R>flatMap(Function<T,Stream<R>>);IntStreamflatMapToInt(Function<T,IntStream>);LongStreamflatMapToLong(Function<T,LongStream>);DoubleStreamflatMapToDouble(Function<T,DoubleStream>);Stream<T>distinct();Stream<T>sorted();Stream<T>sorted(Comparator<T>);Stream<T>peek(Consumer<T>);Stream<T>limit(long);Stream<T>skip(long);

Page 36: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

36 17-214

Streaminterfaceisamonster(2/3)

//TerminalOperationsvoidforEach(Consumer<T>);//OrderedonlyforsequentialstreamsvoidforEachOrdered(Consumer<T>);//OrderedifencounterorderexistsObject[]toArray();<A>A[]toArray(IntFunction<A[]>arrayAllocator);Treduce(T,BinaryOperator<T>);Optional<T>reduce(BinaryOperator<T>);<U>Ureduce(U,BiFunction<U,T,U>,BinaryOperator<U>);<R,A>Rcollect(Collector<T,A,R>);//MutableReductionOperation<R>Rcollect(Supplier<R>,BiConsumer<R,T>,BiConsumer<R,R>);Optional<T>min(Comparator<T>);Optional<T>max(Comparator<T>);longcount();booleananyMatch(Predicate<T>);booleanallMatch(Predicate<T>);booleannoneMatch(Predicate<T>);Optional<T>findFirst();Optional<T>findAny();

Page 37: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

37 17-214

Streaminterfaceisamonster(3/3)

//Staticmethods:streamsourcespublicstatic<T>Stream.Builder<T>builder();publicstatic<T>Stream<T>empty();publicstatic<T>Stream<T>of(T);publicstatic<T>Stream<T>of(T...);publicstatic<T>Stream<T>iterate(T,UnaryOperator<T>);publicstatic<T>Stream<T>generate(Supplier<T>);publicstatic<T>Stream<T>concat(Stream<T>,Stream<T>);}

Page 38: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

38 17-214

Incaseyoureyesaren’tglazedyet

publicinterfaceBaseStream<T,SextendsBaseStream<T,S>>extendsAutoCloseable{

Iterator<T>iterator();Spliterator<T>spliterator();booleanisParallel();Ssequential();//MayhavelittleornoeffectSparallel();//MayhavelittleornoeffectSunordered();//Noteasymmetrywrtsequential/parallelSonClose(Runnable);voidclose();}

Page 39: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

39 17-214

Itkeepsgoing:java.util.stream.Collectors

...toList()

...toMap(...)

...toSet(...)

...reducingBy(...)

...groupingBy(...)

...partitioningBy(...)...

Page 40: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

40 17-214

Optional<T>:anotherwaytoindicatetheabsenceofaresult

ItalsoactsabitlikeadegeneratestreampublicfinalclassOptional<T>{booleanisPresent();Tget();

voidifPresent(Consumer<T>);Optional<T>filter(Predicate<T>);<U>Optional<U>map(Function<T,U>);<U>Optional<U>flatMap(Function<T,Optional<U>>);TorElse(T);TorElseGet(Supplier<T>);<XextendsThrowable>TorElseThrow(Supplier<X>)throwsX;}

Page 41: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

41 17-214

Streamprac9ce

•  GivenaList<String>words,usestreamsto:–  GenerateaList<String>ofallwordscontainingthesubstring"heat"–  Determineifanywordcontainsthesubstring"aoeu"(aboolean)

•  Challenge:Convertsomeopera9oninyourCarcassonnesolu9ontousestreams…

Page 42: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

42 17-214

Streamparallelism:Yourmileagemayvary

•  Considerthisfor-loop(.96srun9me;dual-corelaptop)longsum=0;for(longj=0;j<Integer.MAX_VALUE;j++)sum+=j;

•  Equivalentstreamcomputa9on(1.5s)longsum=LongStream.range(0,Integer.MAX_VALUE).sum();

•  Equivalentparallelcomputa9on(.77s)longsum=LongStream.range(0,Integer.MAX_VALUE).parallel().sum();

•  Carefullyhandcra3edparallelcode(.48s)

Page 43: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

43 17-214

Whentouseaparallelstream,looselyspeaking

•  Whenopera9onsareindependent,and•  Eitherorboth:

–  Opera9onsarecomputa9onallyexpensive–  Opera9onsareappliedtomanyelementsofefficientlyspliSabledata

structures–  Roughly:Numberofelements*Cost/element>>10,000

•  AlwaysmeasurebeforeandaDerparallelizing!

Page 44: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

44 17-214

Whennotto…

•  Useaparallelstream…•  Useastream...

Page 45: Principles of So3ware Construc9on: Objects, Design, and ...charlie/courses/17-214/... · 4/12/2018  · Objects, Design, and Concurrency Part 5: Et cetera Java lambdas and streams

45 17-214

Summary

•  APIdesign:"Funandeasytolearnanduse…?"•  Whentousealambda

–  Always,inpreferencetoCICE•  Whentouseamethodreference

–  Almostalways,inpreferencetoalambda•  Whentouseastream

–  Whenitfeelsandlooksright•  Whentouseaparallelstream

–  Numberofelements*Cost/element>>10,000•  Keepitclassy!

–  Javaisnotafunc9onallanguage