• 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

  1. package com.instanceofjava.java8;

  2. import java.util.function.Function;
  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview questions
  6.  * 
  7.  * Description: java 8 java.util.function.Function example program
  8.  *
  9.  */
  10. public class Java8Function {

  11. public static void main(String[] args) {
  12. Function<Integer, String> function = (n) -> {
  13. return "Number received: "+n;
  14. };

  15. System.out.println(function.apply(37));
  16. System.out.println(function.apply(64));

  17. }

  18. }

Output:

  1. Number received: 37
  2. Number received: 64

#2: Java example program on Function and explain used of addThen() and compose() methods



java 8 function example

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

1 comments for Java 8 java.util.function.Function with example program

Select Menu