Skip to main content

Posts

Showing posts from July, 2014

Introduction to Java 8: Lambda Expressions and Streams

Lambda - Ways of representing functions - pre-built function building blocks - Set of function composition methods Streams  - Wrappers around collections  - map, reduce, filter, forEach  - Lazy Evaluation  - Can be made parallel Note : Lambda is not lisp Stream is not Hadoop Why Lambda's in Java   Ability to pass functions around (Javascript, Lisp, Ruby, Scala etc)   Functional approach proven concise, useful, and parallelizable Java 7 version String[] testStrings = {"", "", ""}; Arrays.sort(testStrings, new Comparator () {    @Override    public int compare(String s1, String s2) {       return (s1.length() - s2.length());    } } ); Lambda version Arrays.sort(testStrings, (s1, s2) -> s1.length() - s2.length() ); Lambda  - replaces anonymous inner class' single method  - No type should be used (Type inferencing)  - Parens optional for single arg (Parens omitting)      button.addActionListener(e -> setBg(C

Java 8 highlights

Chapter 13. New I/O API (NIO.2) NIO.2 was introduced with JDK 7 to provide enhanced file I/O support and access to the default filesystem. NIO.2 is supported by the  java.nio.file  and java.nio.file.attribute  packages. The NIO.2 API is also known as  “JSR 203: More New I/O APIs for the Java Platform.” Popular interfaces that are used from the API are Path ,  PathMatcher ,  FileVisitor , and  WatchService . Popular classes that are used from the API are  Paths  and  Files . The Path Interface The  Path  interface can be used to operate on file and directory paths. This class is an upgraded version of the  java.io.File  class. The following code demonstrates the use of some of the methods of the  Path  interface and the  Paths  class for acquiring information: Path p = Paths . get ( "\\opt\\jpgTools\\README.txt" ); System . out . println ( p . getParent ()); // \opt\jpgTools System . out . println ( p . getRoot ()); // \ System . out . println ( p