Prototype Java Stream Processing in Node.js

We want to prototype in Node.js a stream processing program that will eventually be coded in Java 8+.

A good way to do it is to code in Node.js using arrow functions.

Node.js Collections Processing with Lodash

Lodash is a Node.js (and JavaScript) module for convenient processing of collections, with operators such as map, forEach (each) and filter.

So mapping a collection, squaring each element, we use an arrow function to process each element:

   var _ = require('lodash');
_.map([1, 2, 3, 4], (a) => a * a)

Java 8 Streams

Java 8 streams represents a sequence of elements and supports different kind of operations to perform computations upon those elements:

 List<Integer> myList =
     Arrays.asList(1, 2, 3, 4);

 myList
     .stream()
     .map((a) -> a * a)

The notation of the processing is like the Node.js code above which used arrow functions and Lodash.

Convert the Node.js into Java

Very simple! The arrow notation stays the same, most of the functions have the same name. Just copy-paste and spread type declarations around the original Node.js variables.