Java 8 : Function

What is a Function?

This is a predefined functional interface like Predicatepresent in java.util.function package. It has one abstract method called apply();


1
2
3
interface Function<T,R> {
public R apply(T t);
}


Function example 1: to check the length of the string.

 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Function;

public class FunctionExample1 {

public static void main(String[] args) {
Function<String, Integer> fun = s -> s.length();
System.out.println(fun.apply("ekumeedhelp"));
}

}

Function Chaining example:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.blogspot.ekumeedkiasha;

import java.util.function.Function;

public class FunctionChaining {

public static void main(String[] args) {

int amount = 3;

Function<Integer, Integer> sum = i -> i + i; // 3 + 3 = 6

Function<Integer, Integer> sq = i -> i * i; // 6 * 6 = 36

System.out.println(sum.apply(amount));

System.out.println(sq.apply(amount));

System.out.println(sum.andThen(sq).apply(amount));

System.out.println(sum.compose(sq).apply(amount));

}

}

 

Java 8 : Predicates



Predicate Example to find out all the even numbers in the given array.


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.blogspot.ekumeedkiasha;

import java.util.function.Predicate;

/*
* find out all the even numbers in the given array
*/
public class PredicateExample2 {

public static void main(String[] args) {

int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Predicate<Integer> even = x -> x % 2 == 0;

for (int i : num) {
if (even.test(i)) {
System.out.println(i);
}
}

}

}




Predicate Joining Example:  

Interface predicate has three default methods.

1. and(Predicate p)
2. or(Predicate p)
3. negate()

find out all the even numbers which are greater than 5 in the given array.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.blogspot.ekumeedkiasha;

import java.util.function.Predicate;

/*
* Q1. Find out all the even numbers in the given array.
*
* Q2. Find out all the number which is greater than 5 in the given array.
*/
public class PredicateExample2 {

public static void main(String[] args) {

int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Predicate<Integer> even = x -> x % 2 == 0;

Predicate<Integer> grt = y -> y > 5;

for (int i : num) {
if (grt.or(even).test(i)) {
System.out.println(i);
}

}

}

}


For equality predicate interface has isEqual(Object o) method which is static.

Example: 
 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Predicate;

public class PredicateExample3 {

public static void main(String[] args) {
Predicate<String> name = Predicate.isEqual("Mumbai");
System.out.println(name.test("Mumbai"));
}

}

Java 8 : Predicates


What is Predicate?

Java 8 comes with a concept of functional interface. An interface having only one abstract method and any number of default and static methods is called a functional interface.

A Predicate (boolean-valued function) of one argument which is present in java.util.function package. It is predefined functional interfaces that It has only one abstract method i.e, test().
1
2
3
 interface Predicate<T> {
public boolean test(T t);
}

Predicate Example to check age is greater than 18 or not.

Step: -1
1
2
3
4
5
6
7
public boolean test(Integer age) {
if (age > 18) {
return true;
} else {
return false;
}
}

Step -2
1
2
3
4
5
6
7
8
9
public class PredicateExample {

public static void main(String[] args) {
Predicate<Integer> p = age -> (age > 18);
System.out.println(p.test(21)); // true
System.out.println(p.test(17)); // false
}

}




Predicate Example to check collection is empty or not

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.blogspot.ekumeedkiasha;

import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;

public class PredicateExample {

public static void main(String[] args) {
Predicate<Collection> p = c -> c.isEmpty();

ArrayList<Object> al = new ArrayList<>();
al.add("Monday");

ArrayList<Object> al1 = new ArrayList<>();

System.out.println(p.test(al)); // false
System.out.println(p.test(al1)); // true
}

}


Java 8 : Lambda with Thread Example

Hello Friends,
Thank you for visiting this blog. This is the continuation of my Java 8 video series tutorials which was published in youtube channel ekumeedhelp.

In this post, we will be learning about the use of Java 8 lambda expression in Thread.

Create Thread without using lambda 
Example:- 1

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//https://ekumeedkiasha.blogspot.com/
package com.blogspot.ekumeedkiasha;

public class Main {

public static void main(String[] args) {
Runnable r = new CodeToRun();
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread i = " + i);
}
}
}

class CodeToRun implements Runnable {

@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("CodeToRun Thread i = " + i);
}

}

}


Create Thread with Lambda Example
Example:- 2


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//https://ekumeedkiasha.blogspot.com/
package com.blogspot.ekumeedkiasha;

public class Main {

public static void main(String[] args) {
Runnable r = () -> {
for (int i = 0; i < 10; i++) {
System.out.println("CodeToRun Thread i = " + i);
}
};

Thread t = new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread i = " + i);
}
}
}




Create Thread using Anonymous class
Example:- 3

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//https://ekumeedkiasha.blogspot.com/
package com.blogspot.ekumeedkiasha;

public class Main {

public static void main(String[] args) {

Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("CodeToRun Thread i = " + i);
}
}
});
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread i = " + i);
}
}
}