Skip to content

Commit 3f33387

Browse files
committed
added UnaryOperator & BinaryOperator
1 parent 9753fe7 commit 3f33387

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.learn.functionalInterfaces;
2+
3+
import java.util.Comparator;
4+
import java.util.function.BinaryOperator;
5+
6+
/**
7+
* <p>
8+
* BinaryOperator extends BiFunction Functional Interface.
9+
* Use BinaryOperator, when inputs and output are of same type.
10+
* </p>
11+
*/
12+
public class BinaryOperatorExample {
13+
14+
static Comparator<Integer> comparator = (a, b) -> a.compareTo(b);
15+
16+
17+
public static void main(String[] args) {
18+
19+
BinaryOperator<Integer> binaryOperator = (num1, num2) -> num1 * num2;
20+
System.out.println(binaryOperator.apply(10, 20));
21+
22+
BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(comparator);
23+
System.out.println("Maximum = " + maxBy.apply(10, 20));
24+
25+
BinaryOperator<Integer> minBy = BinaryOperator.minBy(comparator);
26+
System.out.println("Minimum = " + minBy.apply(10, 20));
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.learn.functionalInterfaces;
2+
3+
import java.util.function.UnaryOperator;
4+
5+
/**
6+
* <p>
7+
* Use UnaryOperator when input and output are of
8+
* same type.
9+
* </p>
10+
*/
11+
public class UnaryOperatorExample {
12+
13+
static UnaryOperator<String> unaryOperator = String::toUpperCase;
14+
15+
public static void main(String[] args) {
16+
System.out.println(unaryOperator.apply("test"));
17+
}
18+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This repository contains the basic &amp; advance level examples related to Java
1313
* [BiPredicate](Modern-Java-Examples/src/com/learn/functionalInterfaces/BiPredicateExample.java)
1414
* [Function](Modern-Java-Examples/src/com/learn/functionalInterfaces/FunctionExample.java)
1515
* [BiFunction](Modern-Java-Examples/src/com/learn/functionalInterfaces/BiFunctionExample.java)
16+
* [UnaryOperator](Modern-Java-Examples/src/com/learn/functionalInterfaces/UnaryOperatorExample.java)
17+
* [BinaryOperator](Modern-Java-Examples/src/com/learn/functionalInterfaces/BinaryOperatorExample.java)
1618

1719

1820

0 commit comments

Comments
 (0)