- java.util.function.Function introduced in java 8 for functional programming.
- Function(T,R) will be used in streams where to take one type of object and convert to another and return.
- And this Function() supports methods like apply(), andThen(), compose() and identity() etc.
- Lets see an example on java.util.function.Function.
#1: Java Example program on java.util.function.Function
- package com.instanceofjava.java8;
- import java.util.function.Function;
- /**
- * @author www.Instanceofjava.com
- * @category interview questions
- *
- * Description: java 8 java.util.function.Function example program
- *
- */
- public class Java8Function {
- public static void main(String[] args) {
- Function<Integer, String> function = (n) -> {
- return "Number received: "+n;
- };
- System.out.println(function.apply(37));
- System.out.println(function.apply(64));
- }
- }
Output:
- Number received: 37
- Number received: 64
#2: Java example program on Function and explain used of addThen() and compose() methods
Post code as text, not images
ReplyDelete