Java 8, the power of streams

0
1277

Java 8 has really transformed the way we will think about processing data in Java. Collection are the work horse of data processing and functional Interface, Lambda expressions and streams is a real move forward.  No longer do we need to be kept doing the same iterative loops over and over to process collections.  We can now move to declarative programming where we just pass in the parameters to get it done and it will be done super efficiently using parallel processing of the multi-core CPU of the machine.

This is extremely powerful since streams are processed in one pass as well.  We can do MAP, FILTER AND REDUCE during this pass.  This is in keeping with the same terms used in BIG Data.  While MAP does not break into multi processors as it does in big data, it does take one input and maps it another, FILTER does as it says and REDUCE is like an aggregator.

Let see an example of how easy this to read and how much more efficient this code is.

package com.scq;

import java.util.function.Predicate;
import java.util.stream.Stream;

public class StreamsTest {

public static void main (String [] args ){
Stream<String> stream = Stream.of(“one”, “two”, “ther”, “aklsdjfkljasdkfj”);

Predicate<String> p1 = s -> s.length() > 3;

stream
.filter(p1)
.forEach(s -> System.out.println(s));

}

}

OUT PUT

ther
aklsdjfkljasdkfj.

here is a github project and great learning examples to follow to go into extensive use of functional programming, lambda and the use of reference methods to make shorter and more powerful programs that are easier to read.