Skip to main content

Introduction



Groovy Declarative Style Code

[1,2,3,4,5].findAll { it % 2 == 0 }.each() {value->println "${value*2}"};

There are lot of way to address this stuff in Java 8. So let’s have a look to do the same stuff in Java 8
Example:-

import java.util.Arrays;
import java.util.List;
 
class DeclarativeStyle {
    public static void main(String []args){
        List numbers = Arrays.asList(1,2,3,4, 5, 6);
 
        numbers.stream()                         //A Fancy Collection
           .filter(DeclarativeStyle::isEven)     //Filter the data
           .map(DeclarativeStyle::doubleInteger) //Map new values
           .forEach(System.out::println);        //Iterate & perform the operation
    }
 
    public static String doubleInteger(Integer number){
        return String.valueOf(number * 2);
    }
 
    public static Boolean isEven(Integer number){
        return number%2==0;
    }
}


The first three method calls are just lazy calls, so the list doesn’t iterates till “forEach” is called. Plus, If you do a find operation on a list, then the iteration stops then and there if the element is found.
The most important thing to notice here is Immutability. The existing collection “numbers” isn’t tampered with in any of the operation. So every method call is pure and hence can be performed interchangeably using multithreading. Let’s have a look at this.

Using Stream (Sequential Flow)

import java.util.Arrays;
import java.util.List;
 
import static java.util.stream.Collectors.toList;
 
public class SequentialStreamDemo {
    public static void main(String []args){
        long start = System.currentTimeMillis();
 
        List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
 
        List modifiedNumbers = numbers.stream()
                .filter(SequentialStreamDemo::isEven)
                .map(SequentialStreamDemo::doubleInteger)
                .collect(toList());
 
        modifiedNumbers.forEach(System.out::println);
 
        long end = System.currentTimeMillis();
        System.out.println("Timen =>"+(end-start)/1000f+" seconds");
    }
 
    public static String doubleInteger(Integer number){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return String.valueOf(number * 2);
    }
 
    public static Boolean isEven(Integer number){
        return number%2==0;
    }
}
 
 
 
There are four Even Numbers, so there is a delay of 4 seconds added deliberately by Thread.sleep()
Now let’s have a look at the parallel stream
 
import java.util.Arrays;
import java.util.List;
 
import static java.util.stream.Collectors.toList;
 
public class ParallelStreamDemo {
    public static void main(String []args){
        long start = System.currentTimeMillis();
 
        List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
 
        List modifiedNumbers = numbers.parallelStream()
                .filter(ParallelStreamDemo::isEven)
                .map(ParallelStreamDemo::doubleInteger)
                .collect(toList());
 
        modifiedNumbers.forEach(System.out::println);
 
        long end = System.currentTimeMillis();
        System.out.println("Timen =>"+(end-start)/1000f+" seconds");
    }
 
    public static String doubleInteger(Integer number){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return String.valueOf(number * 2);
    }
 
    public static Boolean isEven(Integer number){
        return number%2==0;
    }
}
 
 
 
Now we are using the parallel streaming which interns use a separate thread 
for every stream. This is faster than the above Sequential Flow code

Comments

Popular posts from this blog

What is declarative programming?

•         Here code is written in such a way that it describes what you want to do, and not how you want to do it. It is left up to the compiler to figure out the how. •         Often defined as any style of programming that is not imperative. •         The program is built from one or more procedures •         It provides a programmer a means to define precisely each step in the performance of a task. Declarative programming contrasts with imperative and procedural programming. Declarative programming is a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed. Functional and logical programming languages are characterized by a declarative programming style. In logical programming languages, programs consist of logical statements, and the program executes by searching for proofs of the statements. Examples •         Example :-    SQL , Prolog , Alpha , Brooks ,