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.

Written by

Open source, Mobile, Web, Cloud, Server - Independent Information Technology and Services Professional http://yoramkornatzky.com kornatzky@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store