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
Post a Comment