Java Streams, Functional Programming Beginners Prerequisites

Java Streams and Functional programming based Java coding is ubiquitous in Java software code repositories. I observed there are many Java developers who still do not get fell of these paradigms (Streams, Functional Programming).

This post is for such Java developers that might need an approach to understand or feel the Java Streams and Functional Programming way of Java coding.

Step 1: Understand Types of Programming

Software Programming can be done in (a) Imperative Approach (b) Declarative Approach

In an imperative approach, you write the code that is “what to do” and “how to do it”.

Example:

List<String> countries = new ArrayList<>();
countries.add("USA");
countries.add("INDIA");

// Here you are mentioning "HOW" for -loop
for(int i = 1; i <+ countries.size(); i++) {
// some business logic goes here
// above logic is "WHAT TO DO WITH THE DATA"
}

In a declarative approach, you write the code that “what to do” and let the Java Streams API decide “How to do the for-loop”

countries.stream().forEach(System.out::println);

Step 2: Understand What is “Higher Order Functions” meaning

Functions that take “a function” as input and returns “a function” are called Higher Order functions.

Step 3: Hands-on Streams now

Do Hands-On of Streams constructs of Java programming language.

While doing Hands-on observe the “declarative approach” way of programming along with your Streams functions.

Step 4: Understand what is “Functional Programming” now

Functional Programming means “passing functions as input” plus Java Streams API doing the “declarative programming” while using the Java Streams constructs

countries.stream().filter(aCountry -> someCountryBasedLogicFunction(aCountry));

Step 5: Master the Java Streams API (java.util.stream package)

  • IntStream, DoubleStream, LongStream
  • Collectors
  • Stream methods (allMatch, anyMatch, count, collect, concat, map, flatMap, iterate, limit, min, max, reduce, seek etc)