Understanding Java 8 streams

Introduction to Java 8 streams and functional programming.

Stream represent a way for using internal iterators in Java 8. And to facilitate the use of streams came the lambda expressions( enabled via functional interface).

 

In the process of evolution of the programming languages from C to C++ to Java , the language became smarter with time.

  • Map<String,String> mapData = new HashMap<>(); the second diamond operator was left blank , as language can infer from the context what it means.
  • Default constructor and super() are automatically injected in the code implicitly.
  • The essence is that programming language from the context can determine what the statement is about and perform the same on behalf of the programmer.
  • Lambda expression are addition to same idea of deriving from context and perform the task.
  • Lambda expression relieve programmer from writing unnecessary code.

 

Lets go though the Java code to see the evolution from external iterators to internal iterators. And how from the context the programming language determines what to do .

Code

  • List Of Integers
List integerList = Arrays.asList(10, 20, 30, 40);

  • Traditional For loop for iterating over the list integer List

for (int i = 0; i < integerList.size(); i++) {
            System.out.println("Traiditional for loop value of i is : " + integerList.get(i));
        }


Traiditional for loop value of i is : 10
Traiditional for loop value of i is : 20
Traiditional for loop value of i is : 30
Traiditional for loop value of i is : 40


[addToAppearHere]
  • Advance for loop for iterating over the list integer List
for (int value : integerList) {
            System.out.println("Advance for loop value of arrayLis is : " + value);
        }



Advance for loop value of arrayLis is : 10
Advance for loop value of arrayLis is : 20
Advance for loop value of arrayLis is : 30
Advance for loop value of arrayLis is : 40

  • Internal iterator using Streams Anonymous class : for iterating over the list integer List
integerList.stream().forEach(new Consumer() {
            @Override
            public void accept(Integer integer) {
                System.out.println("Stream + anonymous class the value is " + integer);
            }
        });


Stream + anonymous class the value is 10
Stream + anonymous class the value is 20
Stream + anonymous class the value is 30
Stream + anonymous class the value is 40

  • Lambda with braces for iterating over the list integer List
integerList.stream().forEach(integer -> {
            System.out.println("lamda with braces value is " + integer);
        });
       


lamda with braces value is 10
lamda with braces value is 20
lamda with braces value is 30
lamda with braces value is 40

  • Lambda without braces for iterating over the list integer List
integerList.stream().forEach(integer -> System.out.println("lamda without braces value is " + integer));

lamda without braces value is 10
lamda without braces value is 20
lamda without braces value is 30
lamda without braces value is 40

  • Calling function directly in lambda for iterating over the list integer List
integerList.stream().forEach(System.out::println);

    
10
20
30
40

[addToAppearHere]

The lambda functions provided the gateway to Functional programming to Java. Imperative programming style is what has always been followed in Java.

Imperative programming:    
Functional programming:

 

Key Take Aways .

  • Imperative programming changes the state of the code by use of statements . Simplest example is assignment operator
  • Functional programming has its origin from Lambda calculus and intent is to avoid maintenance of any kind of state.
  • Functional programming is more closer to mathematical way of thinking where one applies function and gets the output. Function is not dependent on any kind of state.

 

State of i being changed and hence not functional


for (int i = 0; i < integerList.size(); i++) {
            System.out.println("Traiditional for loop value of i is : " + integerList.get(i));
        }

Functional way of iterating (Using lambda)


integerList.stream().forEach(integer -> System.out.println("lamda without braces value is " + integer));