Lamda Static Function Call Java

Lamda Static Function Call Java

The Function interface is defined in java.util.function package and it has only one method apply with the following signature:

public R apply(T t){ }

It takes a generic class T and returns a generic class R. In this example you will how to create static Function which takes List of Integer and add all integer value then return it.

  • FunctionStaticTest.java:
package com.javahonk.lamda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class FunctionStaticTest {
	
	public static void main(String[] args) {
		
		List<Integer> value = new ArrayList<>();
		value.add(55);
		value.add(55);
		value.add(55);		
		Integer value2 = aggegatedQuantity.apply(value);
		System.out.println("Calculated value: "+value2);
		
	}
	
	static Function<List<Integer>, Integer> aggegatedQuantity = t -> {

		int aggregatedQuantity = 0;
		for (Integer vak : t) {
			aggregatedQuantity += vak;
		}
		return aggregatedQuantity;
	};
}
  • Output:

Lamda Static Function Call Java

  • For more information please visit Oracle documentation here

Leave a Reply

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