Java Lambda Expressions

public interface Wisher {
public void perform();
}
public class HelloWorldWisher implements Wisher {
@Override
public void perform() {
System.out.println("Hello there, Good wishes !!");
}
}
public class MainTester {

public void wishesParser(Wisher wisher) {
wisher.perform();
}

public static void main(String args[]) {
MainTester mainTester = new MainTester();
HelloWorldWisher helloWorldWisher = new HelloWorldWisher();
mainTester.wishesPrinter(helloWorldWisher);
}
}
aPieceOfCode =  public void perform() {
System.out.println("Hello there, Good wishes !!");
}
aPieceOfCode =  () -> System.out.println("Good wishes !!");
mainTester.wishesPrinter(() -> System.out.println("Lambda way of Wishing."));
TripleMultiplicator tripleMultiplicator =  (int a) -> a * 3;
MultiplierWith3 multiplierWith3 =  (int a) -> a * 3;
Function<Integer, Integer> mulBy3 =  (int a) -> a * 3;
public interface TripleMultiplicator {
public int anyFunctionNameIsFine(int a);
}
TripleMultiplicator tripleMultiplicator = (int a) -> a * 3;
public interface MultiplierWith3 {
public int anyFunctionNameIsFine(int a);
}
MultiplierWith3 multiplierWith3 = (int a) -> a * 3;
public interface Function<T, V> {
public V apply(T t);
}
Function<Integer, Integer> mulBy3 = (int a) -> a * 3;
AdditorMahashay additor =  (int a, int b, int c) -> a + b + c;public interface AdditorMahashay {
public int anyFunctionNameIsFine(int a, int b, int c);
}
DivisorHeisenberg divisor =  (int a, int b) -> {
if (b ==0) return 0;
return a/b;
}
public interface DivisorHeisenberg {
public int anyFunctionNameIsFine(int a, int b);
}
LengthFinder lenOfString =  (String s) -> s.length();public interface LengthFinder {
public int anyFunctionNameIsFine(String a);
}
Wisher helloWorldWisher = new HelloWorldWisher();
Wisher lambdaWisher = () -> System.out.println("Lambda wishes !!");
Wisher innerClassWisher = new Wisher() {
@Override
public void perform() {
System.out.println("Inner Class way of Wishing.");
}
};
public interface Wisher {
public void perform();
}
helloWorldWisher.perform(); // Works fine.lambdadWisher.perform(); // Works fine as well.
Thread anonymousClassWayOfThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Inner Class way of Threading.");
}
});

anonymousClassWayOfThread.start();

Thread lambdaWayOfThread =
new Thread(() -> System.out.println("Lambda way of Threading."));
lambdaWayOfThread.start();
  • We are using a Java8 provided functional interface with name as ‘BiConsumer’. This interface have a built-in method with name as ‘accept’ which takes-in 2 parameters and returns nothing.
  • We are supplying the definition of the method i.e. implementation to be performed as an lambda expression. Here, we are suggesting to print the result of 2 integer numbers.
  • Invocation of the pre-provided method i.e. ‘accept’ happens from within a for loop.
public static void main(String args[]) {
int [] theseNumbers = {1, 2, 3, 4};
int key = 2;
process(theseNumbers, key, (v, k) -> System.out.println(v * k));
}

public static void process(int[] theseNumbers, int key, BiConsumer<Integer, Integer> biConsumer) {
for(int i : someNumbers) {
biConsumer.accept(key, i);
}
}
for (Integer num : someeNumbers) {
System.out.println(num);
}
List<Integer> someeNumbers = Arrays.asList(1, 2, 3, 4, 5);
someeNumbers.forEach(thisNumber -> System.out.println(thisNumber));
List<Integer> someeNumbers = Arrays.asList(1, 2, 3, 4, 5);
someeNumbers.stream().filter(thisNum -> (thisNum%2 == 0)).forEach(thisNumber -> System.out.println(thisNumber));
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Computer Science Undergraduate Guide To The 2020 Quarantine

Top 5 Enterprise IoT Platforms for the Growth of Your Business

A closer look at Networking with Kubernetes !

Spek2 — After Migration

9 Web Development Trends to Skip in 2019

Skaffold, a quick run-through on Windows

All you need to know about Pointers-Part-2

Getting started with Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

What is GitOps ?

CS373 Spring 2022: Joriann Bassi

Journey in 3 Pillar Global