lambdas and streams hands on lab

28

Upload: simon-ritter

Post on 02-Jul-2015

241 views

Category:

Software


4 download

DESCRIPTION

Slides used at EclipseCon Europe for the Lambdas and Streams hands on lab. Setup and some discussion of solutions.

TRANSCRIPT

Page 1: Lambdas And Streams Hands On Lab
Page 2: Lambdas And Streams Hands On Lab

Lambdas  &  Streams  Laboratory  

Simon  Ri2er  Oracle  Corp.    Twi2er:  @speakjava  

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Page 3: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Lambdas  and  FuncMons  Library  Review  

 

Page 4: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Lambda  Expressions  •  Lambda  expression  is  an  anonymous  funcMon  •  Think  of  it  like  a  method  – But  not  associated  with  a  class  • Can  be  used  wherever  you  would  use  an  anonymous  inner  class  – Single  abstract  method  type  

•  Syntax  – (  [optional-­‐parameters]  )  -­‐>  body  •  Types  can  be  inferred  (parameters  and  return  type)  

Page 5: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Lambda  Examples  

SomeList<Student>  students  =  ...  double  highestScore  =      students.stream().                        filter(Student  s  -­‐>  s.getGradYear()  ==  2011).                        map(Student  s  -­‐>  s.getScore()).                        max();  

Page 6: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Method  References  •  Method  references  let  us  reuse  a  method  as  a  lambda  expression  

FileFilter  x  =  (File  f)  -­‐>  f.canRead();  

FileFilter  x  =  File::canRead;  

Page 7: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

The  Stream  Class  

• Stream<T>  – A  sequence  of  elements  supporMng  sequenMal  and  parallel  operaMons  

• A  Stream  is  opened  by  calling:  – Collection.stream()  – Collection.parallelStream()  • Many  Stream  methods  return  Stream  objects  – Very  simple  (and  logical)  method  chaining  

 

java.u&l.stream  

Page 8: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Stream  Basics  • Using  a  Stream  means  having  three  things  • A  source  – Something  that  creates  a  Stream  of  objects  

•  Zero  or  more  intermediate  objects  – Take  a  Stream  as  input,  produce  a  Stream  as  output  – PotenMally  modify  the  contents  of  the  Stream  (but  don’t  have  to)  

• A  terminal  operaMon  – Takes  a  Stream  as  input  – Consumes  the  Stream,  or  generates  some  other  type  of  output  

Page 9: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Stream  Usage  • MulMple  operaMons  available  – collect,  filter,  count,  skip,  limit,  sorted  – map  (and  map  to  types,  e.g.  mapToInt)  – flatMap  maps  each  element  in  a  Stream  to  possibly  mulMple  elements  •  e.g.  flatMap(line  -­‐>  Stream.of(line.split(REGEXP));  

List<String>  names  =  Arrays.asList(“Bob”,  “Alice”,  “Charlie”);  System.out.println(names.      stream().      filter(e  -­‐>  e.getLength()  >  4).      findFirst().      get());  

Page 10: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

java.uMl.funcMon  Package  • Predicate<T>  – Determine  if  the  input  of  type  T  matches  some  criteria

• Consumer<T>  – Accept  a  single  input  argumentof  type  T,  and  return  no  result  

• Function<T,  R>  – Apply  a  funcMon  to  the  input  type  T,  generaMng  a  result  of  type  R  • Supplier<T>  – A  supplier  of  results  of  type  T  • Plus  several  more  type  specific  versions  

Page 11: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

The  iterable  Interface  

• One  method  – forEach()  – The  parameter  is  a  Consumer  

Used  by  most  collec&ons  

wordList.forEach(s  -­‐>  System.out.println(s));    wordList.forEach(System.out::println);  

Page 12: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Files  and  Lines  of  Text  • BufferedReader  has  new  method  – Stream<String>  lines()  

• HINT:  Test  framework  creates  a  BufferedReader  for  you  

Page 13: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Map  Values  in  a  Stream  Maps  and  FlatMaps  

Map  

FlatMap  

Input  Stream  

Input  Stream  

1  to  1  mapping  

1  to  many  mapping  

Output  Stream  

Output  Stream  

Page 14: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Useful  Stream  Methods  • collect  (terminal)  • filter  (intermediate)  • count  (terminal)  • skip,  limit  (intermediate)  • max  (terminal)  • getAsInt  (terminal)  

Page 15: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Gebng  Started  • Open  the  LambdasHOL  project  in  Eclipse  •  The  exercises  are  configured  as  tests  •  Edit  each  test’s  method  – Remove  the  @Ignore  annotaMon  

• Run  the  tests    – Right-­‐click  the  Exercises.java  file  and  select  Run  as  >  JUnit  Test  • Make  the  tests  pass  •  Simple!  

Page 16: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Access  To  The  Files  

1.  USB  keys  at  front  2.  www.github.com/speakjava/Lambda_Lab-­‐EclipseCon  3. Micro  router  (10.0.1.254)  – ESSID:  NANO_NOMIS  – Workgroup:  NOMIS  

16  

Page 17: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Let’s  Go!    

Page 18: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

SoluMons    

Page 19: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       19  

First  character  of  each  word  concatenated  Exercise  1  

StringBuilder  sb  =  new  StringBuilder();  input.forEach(s  -­‐>  s.append(s.charAt(0)));  String  result  =  sb.toString();  

String  result  =  input.stream()      .map(s  -­‐>  s.substring(0,  1))      .reduce("",  (a,  b)  -­‐>  a  +  b);  

Page 20: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       20  

Map  whose  keys  are  first  le@er,  values  are  sum  of  lengths  Exercise  5  

Map<String,  Integer>  result  =  new  TreeMap();    list.forEach(s  -­‐>      result.merge(s.substring(0,  1),                                s.length(),                                Integer::sum));  

Page 21: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       21  

Find  the  length  of  the  longest  line  Exercise  9  

int  longest  =  reader.lines()      .mapToInt(String::length)      .max()      .getAsInt();  

Page 22: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       22  

Find  the  longest  line  Exercise  10  

String  longest  =  reader.lines()      .max(comparingInt(String::length))      .get();  

Page 23: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       23  

Select  the  set  of  words  whose  length  is  greater  than  the  word's  posi&on  Exercise  11  

List<String>  result  =  IntStream.range(0,  input.size())      .filter(pos  -­‐>  input.get(pos).length()  >  pos)      .mpToObj(pos  -­‐>  input.get(pos))      .collect(toList());  

Page 24: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       24  

Convert  a  list  of  strings  into  a  list  of  characters  Exercise  13  

List<Character>  result  =  input.stream()      .flatMap(word  -­‐>  word.chars().mapToObj(i  -­‐>  (char)i))      .map(c  -­‐>  (Character)c)      ,collect(toList());  

Page 25: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       25  

Sort  unique,  lower-­‐cased  words  by  length,  then  alphabe&cally  within  length  Exercise  17  

List<String>  result  =  reader.lines()      .flatMap(line  -­‐>  Stream.of(line.split(REGEXP)))      .map(String::toLowerCase)      .distinct()      .sorted(comparingInt(String::length)          .thenComparing(naturalOrder()))      .collect(toList());  

Page 26: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       26  

Count  total  number  of  words  and  dis&nct  lower-­‐cased  words  in  one  pass  Exercise  18  

LongAdder  adder  =  new  LongAdder();    long  distinctCount  =  reader.lines()      .flatMap(line  -­‐>  Stream.of(line.split(REGEXP)))      .map(String::toLowerCase)      .peek(s  -­‐>  adder.increment())      .distinct()      .count();  

Page 27: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.       27  

Compute  the  value  of  21!  (which  needs  to  use  BigInteger)  Exercise  19  

BigInteger  result  =  IntStream.rangeClosed(1,  21)      .mapToObj(n  -­‐>  BigInteger.valueOf(n))      .reduce(BigInteger.ONE,  (m,  n)  -­‐>  m.multiply(n));      T  merge(T  identity,  BiFunction  accumulator)    ===    T  result  =  identity;  for  (T  element  :  this  stream)      result  =  accumulator.apply(result,  element)  return  result;    

Page 28: Lambdas And Streams Hands On Lab

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.      

Graphic  Sec&on  Divider