-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Java Stream API
The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.
inteface Dispenable{
public void disp();
}
public class LambdaExpressionExample{
public static void main(String args[]){
Dispenable d=()->{
System.out.println("Dispensing Water");
};
Dispenable d1=(String juice)->{
System.out.println("Dispensing"+juice);
};
d.disp();
d1.disp("Juice");
}
}Functional interfaces were introduced in Java 8. A functional interface can contain only one abstract method and it can contain any number of static and default (non-abstract) methods. Abstract methods are methods that do not require implementation during their declaration and must be overridden by the class implementing the interface.
@FunctionalInterface
inteface Sample{
int calculate(int val);
}
public class Main{
public static void main(String[] args){
Sample s = (val) -> val * val;
System.out.println(s.calculate(5));
}
}Consumer - The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don’t return any value.
It contains an abstract accept() and a default andThen() method. It can be used as the assignment target for a lambda expression or method reference.
The Consumer Interface accepts a single argument and does not return any result.
import java.util.function.Consumer;
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
public class Main {
public static void main(String[] args) {
Consumer<String> print = x -> System.out.println(x);
print.accept("java"); // java
print.accept("python"); // python
}
}Predicate
Predicate is a functional interface in Java that accepts exactly one input and can return a boolean value. Before going into it, we first need to understand the Functional Interface.
It is a functional interface whose functional method is the test()
BiPredicate
BiPredicate is a functional interface in Java that accepts two inputs and can return a boolean value. It is similar to the Predicate interface. The only difference is that it takes two inputs instead of one.
It is a functional interface whose functional method is the test()
Generally they are used in the following cases:
- To filter a collection of objects
- To check if a given object satisfies a certain condition
- To check if a given object satisfies multiple conditions
import java.util.function.Predicate;
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
class Main{
public static void main(String[] args){
Predicate<Integer> p = (val) -> val > 10;
System.out.println(p.test(5)); // false
System.out.println(p.test(15)); // true
BiPredicate<Integer, Integer> p1 = (val1, val2) -> val1 > val2;
System.out.println(p1.test(5, 10)); // false
System.out.println(p1.test(15, 10)); // true
}
}Function
Function is a functional interface, it takes an argument (object of type T) and returns an object (object of type R). The argument and output can be a different type.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}BiFunction
The BiFunction interface is also a pre-defined functional interface that takes two parameters and returns a result. It is similar to the Function interface except it takes two parameters.
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}UnaryOperator
java.util.function.UnaryOperator is a java 8 functional interface that extends java.util.function.Function. UnaryOperator is used to work on a single operand. It returns the same type as an operand. UnaryOperator can be used as lambda expression to pass as an argument. While defining UnaryOperator, we need to define Function.apply(Object) where Function will be the instance of UnaryOperator
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
}
public class UnaryOperator{
public static void main(String[] args){
UnaryOperator<Integer> u = (val) -> val * val;
System.out.println(u.apply(5)); // 25
}
}BinaryOperator
java.util.function.BinaryOperator is a java 8 functional interface that extends java.util.function.BiFunction. BinaryOperator is used to work on two operands. It returns the same type as an operand. BinaryOperator can be used as lambda expression to pass as an argument. While defining BinaryOperator, we need to define BiFunction.apply(Object, Object) where BiFunction will be the instance of BinaryOperator
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {}
public static void main(String[] args){
BinaryOperator<Integer> b = (val1, val2) -> val1 + val2;
System.out.println(b.apply(5, 10)); // 15
}Streams are designed to work with Java lambda expressions. To work with stream . First we need to convert the collection into stream. We can use the stream() method to convert the collection into stream. The stream() method is present in the Collection interface.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Main{
public static void main(String[] args){
List<String> list = new ArrayList<>();
list.add("java");
list.add("python");
list.add("c++");
list.add("c");
list.add("javascript");
Stream<String> stream = list.stream();
Stream stream1 = list.stream();
stream.forEach(System.out::println);
stream.map(item -> item.toUpperCase());
stream.filter(item -> item.startsWith("c"));
stream.sorted();
}
}