java 8 stream and c# 3.5

Click here to load reader

Post on 19-Jul-2015




3 download

Embed Size (px)



Java 8 Stream & C# 3.5Trn Duy Quang May 2014AgendaSome notable new things in Java 8Comparison with LINQ in C# 3.5!21Some new things in Java 8Notable thingsLambda expressionsDefault methodsMethod referenceStreamOptionalNew Date & Time APIFunctional programming

Reference (blog post): Everything about Java 84Lambda expression5(x, y) -> x + yPassing function aroundDynamically (usually weakly) typed: JavascriptStrongly typed: Ruby, Scala, Closure

6In 1974, Liskov and Zilles described a strong-typed language as one in which "whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function."[1]Jackson wrote, "In a strongly typed language each data area will have a distinct type and each process will state its communication requirements in terms of these types."[2]6Why lambda in Java now?Clearer than anonymous inner class

Basic for stream libraryshapes.forEach(s -> s.setColor(Color.RED));A step toward functional programming !7

Main advantage of lambdasConcise & expressive8

New way of thinkingEncourage functional programmingMany classes of problems are easier to solveCode is clearer to read, simpler to maintain

9What we actually get?An instance of a class that implements the interface that was expected in that placeRemember this?

Interface that has exactly one abstract method!Functional Interface | Single Abstract Method10

Shortening lambda syntaxOmit parameter types

Expressions instead of blocks

Single arguments? Omit parenthesis!11

Even better with high-order functionHigh-order function: method return lambdascomparing method of Comparator need function specifying how to extract the key12

Method reference13Core idea behindMake lambda more succinct!Rather than -> Math.sin(x));Can be shorthand;14Predicate15Core idea behindBoolean test(T t)A function to test condition16

Benefit: FlexibilityEven better with stream (later slides)17

Similar thingsPredicate: boolean test(T t) Check a conditionConsumer: void consume(T t)Perform action with the given objectBiConsumer: with two parametersFunction: R change(T t)Take an object type T and return new object type RBiFunction: with two parametersSupplier: T supply(T t)Return an object with the same type1818More convenient helper classesIntConsumerIntFunctionIntPredicateIntSupplier19Stream20StreamWrapper around data sources: arrays, collectionsUse lambdasSupport map/reduceLazy evaluationMade parallel atutomatically by compiler21

Making streamsFrom individual valuesStream.of(val1, val2,)From arrayStream.of(names), List (and other collections) a StringString.chars, another streamdistinct, filter, map, limit, sorted, substream2222Turn stream to other data structuresArrayemployees.toArray(Employee[]::new);Listnames.collect(Collectors.toList());Setnames.collect(Collectors.toSet());

23i vi mng thng thng l EntryType[]::new, nhng thng thng l nhn mt Supplier vi i s l mt s nguyn (size) v tr v mt mng rng. Cng c hm toArray khng i s, tuy nhin n tr v Object[], v ta khng th cast Object[] sang Blah[] cho d cc thnh phn ca mng c kiu l Blah

Thng l phi import static*232Comparison with LINQ in C#Restriction Operators251. FilteringFind names where am occurs26

2. Indexed Filtering (tricky)Find names where length =13.1.2)Change project language level to 8.0 (F4)56

ReferenceBlog post: Java Streams Preview vs .Net High-Order Programming with LINQSlide: Evolution of JavaSlide: Lambda expressions & Stream in Java 8Slide: Java 8 Stream Tutorial Part 1Slide: Java 8 Stream Tutorial part 2

Just for fun (Youtube Video 20): Javapocalypse ^^57