Showing posts with label java8. Show all posts
Showing posts with label java8. Show all posts

Saturday, May 2, 2020

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