Function Method Java Example

Function Method Java Example

java.util.function in Java 8 introduced many useful interfaces which can be use effectively for lambda expressions and method references. Here we will see how to use Function interface

Interface Function<T,R>

Parameters:

T – type of the input to the function
R – type of the result of the function

It takes two value <T,R> where first one is input and last one is return type result. In this example I will show you how to use as method:

  • FunctionAsMethod.java:
package com.javahonk;

import java.util.function.Function;

public class FunctionAsMethod {

	public static void main(String[] args) {
		
		System.out.println(function.apply("Java Honk"));
		
	}

	public static Function<String, Double> function = (e) -> {
		
		if (e.equalsIgnoreCase("Java Honk")) {
			return 55.0;
		}
		
		return 0.0;
		
	};
}

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *