Sunday, May 17, 2020

CompletableFuture API in Java 9

In Java 9, following additions were made in CompletableFuture API:

1. Executor defaultExecutor()
2. CompletableFuture<U> newIncompleteFuture()
3. CompletableFuture<T> copy()
4. CompletionStage<T> minimulCompletionStage()
5. CompletableFuture<T> completeAsync(Supplier <? extends T> supplier, Executor executor)
6. CompletableFuture<T> completeAsync(Supplier <? extends T> supplier)
7. CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)
8. CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)

Examples:
1. Creating a completed CompletableFuture:

2. Running a simple Asynchronous Stage:

3. Applying a Function:
4.

Saturday, May 2, 2020

The Famous Producer & Consumer Problem in Java

Problem Statement:

There are two processes, a producer & a consumer, that share a common buffer with a limited size.
The producer produces data and stores it in buffer while the consumer consumes that data from by removing it from the buffer.
Having two processes running in parallel we need to make sure that producer will not put new data when the buffer size is full and consumer will not remove data from the buffer if the buffer size is empty.

Problem with Java classic approach:
The output turns out in exception:
The classic solution to the above problem: The output:

Stream API in Java

Stream in Java:

Stream API is used to process collection of objects. It is a sequence of objects that supports various methods which can be pipelined to produce desired result.

Following are the characteristic of the Stream -
  • Sequence of elements: A stream provides set of elements of specific type in a sequential manner. It get/commutes elements in demand. It never stores the elements
  • Source: Stream takes Arrays, Collections or I/O resource as input resource. 
  • Aggregate Operations: Stream supports aggregate operations like filter, map, limit, reduce, find, match etc.
  • Pipelining: Most of the stream operation returns stream itself so that their results can be pipelined. This operations are called as Intermediate Operations and their function is to take input, process them and return output to the target. collect() method is the terminal operation which is normally present at the end of the pipelining operation to mark the end of the Stream.
  • Automatic iteration: Stream operations does the iteration internally over the source elements provided.
In Java 8 Collection interface has two methods to generate the Stream:
  1. stream(): Returns a sequential stream.
  2. parallelStream(): Returns the parallel stream.
Example: 


The output will be:

Friday, May 1, 2020

Java 8 Functional Interface


A Functional interfaces have single functionality to exhibit. Just like compareTo function is used for comparison purpose.

1. Function<T, R>

Function takes one argument of type T and Returns one argument of type R.

Here,
T -> Type of input to the function.
R -> Type of output/result of the function.

Example: 

Function<String, Integer> getStringLengthFunction = x->x.length()
Integer length = getStringLengthFunction.apply("abhay");
System.out.println(length);

The output will be: 5

2. Chaining Function <T, R>


We can chain multiple functions:

Example: 

Function<String, Integer> getStringLengthFunction = x->x.length();
Function<Integer,Integer> multiplyFunction = x->x*2;

Integer value = getStringLengthFunction.andThen(multiplyFunction).apply("abhay");
System.out.println(value);

The output will be: 10

3. BiPredicate Function <T, R>


The BiPredicate function takes two arguments as input and returns boolean value. Meaning you can input two arguments, process it and return the result in boolean.

Example:

BiPredicate <String,Integer> validateLength = (x,y) -> {
          return x.length() == y;
};

boolean isCorrect = validateLength.test("abhay",5);
System.out.println(isCorrect);

isCorrect = validateLength.test("abhay",10);
System.out.println(isCorrect);

The output will be: 
true
false

4. Consumer <T>


The Consumer function takes one argument as input and doesn't return anything.

Example:

Consumer <String> value = x ->{
          System.out.println("The value is: "+x);
};

value.accept("abhay");

The output will be: The value is: abhay

5. Supplier <R>


The Supplier is opposite of the Consumer, it takes nothing but returns object.

Example:

Supplier<LocalDateTime> s = () -> LocalDateTime.now();
LocalDateTime localDateTime = s.get();
System.out.println(localDateTime);

The output will be: 2020-05-02T10:26:38.454

Terraform Cheat Sheet [WIP]

Installing Terraform