Skip to main content


Showing posts from July, 2014

Introduction to Java 8: Lambda Expressions and Streams

- Ways of representing functions
- pre-built function building blocks
- Set of function composition methods

 - Wrappers around collections
 - map, reduce, filter, forEach
 - Lazy Evaluation
 - Can be made parallel

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() {
   public int compare(String s1, String s2) {
      return (s1.length() - s2.length());

Lambda version
Arrays.sort(testStrings, (s1, s2) -> s1.length() - s2.length());

 - replaces anonymous inner class' single method
 - No type should be used (Type inferencing)  - Parens optional for single arg (Parens omitting)      button.addActionListener(e -> setBg(Color.RED));  - can refer to local, instance variable …

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 andjava.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 arePath, 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 class. The following code demonstrates the use of some of the methods of the Path interface and the Paths class for acquiring information: Pathp=Paths.get("\\opt\\jpgTools\\README.txt");System.out.