Java is to JavaScript what car is to Carpet
– Chris Heilmann
Hello to all the java enthusiast out there! Today I will talk about lambda's in java and about colon operators.
So What exactly is a lambda?
Simply put, it is a function without any name, any return type or any modifier. Let us understand with a simple example
public void divide(int a , int b) {
System.out.println(a/b);
}
- Name : divide
- Modifier : public
(int a , int b) {
System.out.println(a/b);
}
If you have some knowledge about lambda's, you might think - Hey ! Where are those arrow things? We add them after the parameters :
(int a , int b) -> {
System.out.println(a/b);
}
(a , b) -> {
System.out.println(a/b);
}
How do we get invoke these methods and where do we declare them?
For this we use the concept of funtional interfaces.A functional interface is an interface that contains single abstract method.We can have any number of static and default methods
How to create a functional interface?
We use @FunctionalInterface from java.lang package.Let us dig deeper in this concept with an example without using lambda-
@FunctionalInterface
public interface interfaceWithoutLambda {
public void test(); ---> This is an abstract method
}
We create another class to implement this interface
class classWithoutLambda implements
interfaceForLambda {
@Override
public void test() {
System.out.println("This is a trivial way to implement before lambda");
}
}
Then we create a main class as
class mainClass {
public static void main(String args[]) {
classWithoutLambda obj = new classWithoutLambda();
obj.test();
}
}
// Here the interface definition remains same just changing the name for you
to understand better
@FunctionalInterface
public interface interfaceWithLambda {
public void test(); ---> This is an abstract method
}
// In Main
class mainClass {
public static void main(String
args[]) {
interfaceWithLambda inter = ()
=>
System.out.println("This is how we
implement with lambda");
inter.test();
}
}
Here we can compare both the implementations and see how
drastically the code is reduced and readability increased. We have a bunch of interfaces provided to us, which contain one abstract
method. We can leverage these interfaces according to our use case.Predicate<T>
Predicate is used for validations such as age > 20. So it has a
abstract test method which we can use in our lambda. As we know that an
interface with only one abstract method can be used for creating a
lambda function.
interface Predicate
{
boolean test(T t);
// abstract method test, which we will use for our lambda.
}
class mainClass {
public static void main(String args[]) {
Predicate <Integer> p = age ->
age > 25; --> This will return true if age is greater
than 25
//Note : We just implemented test abstract method in the line above
where we passed age as a parameter and used lambda to provide
implementation for test method of Predicate interface. Now we call
this method with our new provided implementation as
p.test(26); ----> True will be returned if you print
it
p.test(22); ----> False will be returned if you print it
}
}
Function<T,R>
- This is also a functional interface
- Unlike predicate, which returns a boolean , this can return anything after performing an operation on the values passed to it.
interface Function<T,R> {
R apply(T t);
}
interface Function<T,R>
{
R apply(T t);
}
class mainClass {
public static void main(String
args[]) {
Function<String,Integer> fun
= str -> str.length();
// Lambda provided implementation for
apply method
// Beautifully called the apply
method
int
len = fun.apply(s);
}
}
Consumer<T>
This interface only consumes and returns nothing. Let us look at its syntax :
interface Consumer<T> {
void accept(Tt);
}
How we will use it in actual code? Let us look at an example.
class mainClass {
public static void main(String args[]) {
Consumer<String> c = s ->
System.out.println(s);
c.accept("whats up");
}
}
Supplier<R>
Supplies but consumes nothing. Let us look at how this predefined interface works :interface Supplier<R> {
R get();
}
class mainClass {
public static void main(String [] args) {
Supplier<String> s = () -> "Value to return";
s.get(); ---> will return "Value to return"
}
}
Comments
Post a Comment