Jump to content

Functional interfaces examples (Function, Consumer, Predicate, Supplier, etc....)


Botre

Recommended Posts

"Functional interfaces provide target types for lambda expressions and method references."

 

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

 

In order to further my functional programming adventures I wrote a couple of (arbitrary and overly simplified) examples for almost every interface from java.util.function.

 

Hope these help you understand / recognize the package in question a bit more. 

package org.dreamstream.sandbox.example.function;

import java.util.Random;
import java.util.function.*;

public class FunctionPackageExample {

	public static void main(String[] args) {
		
		/**
		 * Predicate
		 * Represents a predicate (boolean-valued function) of one or two arguments.
		 */
		
		final Predicate<String> IS_EMPTY = s -> s.isEmpty();
		System.out.println(IS_EMPTY.test("")); // = true
		
		final BiPredicate<String, String> ARE_EQUAL = (s1, s2) -> s1.equals(s2);	
		System.out.println(ARE_EQUAL.test("Test1", "Test2")); // = false
		
		final IntPredicate IS_EVEN = i -> i % 2 == 0;
		System.out.println(IS_EVEN.test(2)); // = true
		
		final LongPredicate	CAN_TRUNCATE_TO_INT = l -> l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE ;
		System.out.println(CAN_TRUNCATE_TO_INT.test(Integer.MAX_VALUE + 1l)); // = false
		
		final DoublePredicate IS_CLOSE_TO_ZERO = d -> Math.abs(d) < 2 * Double.MIN_VALUE;
		System.out.println(IS_CLOSE_TO_ZERO.test(0.0)); // = true
		
		/**
		 * Function
		 * Represents a function that accepts one or two arguments and produces a result.
		 */
		
		Function<String, Integer> CAST = s -> Integer.parseInt(s);
		System.out.println(CAST.apply("1") + 2); // = 3
		
		BiFunction<Integer, Integer, String> SUM_TO_STRING = (i1, i2) -> Integer.toString(i1 + i2);
		System.out.println(SUM_TO_STRING.apply(5, 10) + "Hi"); // = 15Hi
		
		IntFunction<Double> AS_DOUBLE = i -> i * 1d;
		System.out.println(AS_DOUBLE.apply(1)); // = 1.0
		
		LongFunction<Integer> AS_INTEGER = l -> (int)l;
		System.out.println(AS_INTEGER.apply(Long.MAX_VALUE)); // = -1
		
		DoubleFunction<Integer> DOUBLE_AS_INTEGER = d -> (int)d;
		System.out.println(DOUBLE_AS_INTEGER.apply(2.5)); // = 2
		
		/**
		 * Consumer
		 * Represents an operation that accepts one or two input arguments and returns no result.
		 */
		
		final Consumer<String> PRINT = s -> System.out.println(s);
		PRINT.accept("Yes / No"); // = "Yes / No"
		
		final BiConsumer<String, Integer> PRINT_STRING_AND_INTEGER = (s, i) -> System.out.println("String: " + s + " Ingeger: " + i);
		PRINT_STRING_AND_INTEGER.accept("Test", 3); // = "String: Test Ingeger: 3"
		
		final IntConsumer PRINT_I = i -> System.out.println(i);
		PRINT_I.accept(3); // = 3
		
		final LongConsumer PRINT_L = l -> System.out.println(l);
		PRINT_L.accept(2); // = 2
		
		final DoubleConsumer PRINT_D = d -> System.out.println(d);
		PRINT_D.accept(2.5); // = 2.5
		
		final ObjIntConsumer<String> PRINT_I_S = (s, i) -> System.out.println(s + " " + i);
		PRINT_I_S.accept("A", 1); // = "A 1"
		
		final ObjLongConsumer<String> PRINT_L_S = (s, l) -> System.out.println(s + " " + l);
		PRINT_L_S.accept("B", 2); // = "B 2"
		
		final ObjDoubleConsumer<String> PRINT_D_S = (s, d) -> System.out.println(s + " " + d);
		PRINT_D_S.accept("C", 2.5); // = "C 2.5"
		
		/**
		 * Supplier
		 * Represents a supplier of results of a specific type.
		 */
		
		final Supplier<Double> SUPPLY_RANDOM = () -> Math.random();	
		System.out.println(SUPPLY_RANDOM.get()); // =  A double value with a positive sign, greater than or equal to 0.0 and less than 1.0
		
		final BooleanSupplier SUPPLY_TRUE = () -> true;
		System.out.println(SUPPLY_TRUE.getAsBoolean()); // = true
		
		final IntSupplier SUPPLY_EVEN = () -> new Random().nextInt() & -2;
		System.out.println(SUPPLY_EVEN.getAsInt()); //  = An even int value
		
		final LongSupplier SUPPLY_ZERO = () -> 0;
		System.out.println(SUPPLY_ZERO.getAsLong()); // = 0
		
		final DoubleSupplier SUPPLY_MIN = () -> Double.MIN_VALUE;
		System.out.println(SUPPLY_MIN.getAsDouble()); // = 4.9E-324
		
		/**
		 * ToNumber
		 * Represents functions that accept one or two two arguments and produces a value of a number-type.
		 */
		
		final ToIntFunction<String> CHARACTER_COUNT = s -> s.length();
		System.out.println(CHARACTER_COUNT.applyAsInt("Five5")); // = 5
		
		final ToIntBiFunction<String, String> SUM_CHARACTER_COUNT = (s1, s2) -> s1.length() + s2.length();
		System.out.println(SUM_CHARACTER_COUNT.applyAsInt("AB", "C")); // = 3
		
		final ToLongFunction<String> THOUSAND_TIMES_CHARACTER_COUNT = s -> s.toCharArray().length * 1000l;
		System.out.println(THOUSAND_TIMES_CHARACTER_COUNT.applyAsLong("Five5")); // = 5000
		
		final ToLongBiFunction<Long, String> SUM = (l, s) -> l + Integer.parseInt(s);
		System.out.println(SUM.applyAsLong(300l, "1")); // = 301
		
		final ToDoubleFunction<String> HALF_CHARACTER_COUNT = s -> s.length() / 2d;
		System.out.println(HALF_CHARACTER_COUNT.applyAsDouble("One")); // = 1.5
		
		final ToDoubleBiFunction<String, String>  AVERAGE_CHARACTER_COUNT = (s1, s2) -> (s1.length() + s2.length()) / 2d;
		System.out.println(AVERAGE_CHARACTER_COUNT.applyAsDouble("One", "Five")); // = 3.5
		
		/**
		 * Operator
		 * Represents an operation on one or two operands of the same type, producing a result of the same type as the operand(s).
		 */
		
		final UnaryOperator<String> REVERSE = s -> new StringBuilder(s).reverse().toString();
		System.out.println(REVERSE.apply("test")); // = "tset"
		
		final BinaryOperator<String> CONCAT = (s1, s2) -> s1.concat(s2);
		System.out.println(CONCAT.apply("Hello, ", "World!")); // = "Hello, World!"
		
		final IntUnaryOperator DOUBLE = i -> i * 2;
		System.out.println(DOUBLE.applyAsInt(5)); // = 10
		
		final IntBinaryOperator INTEGER_SUM = (i1, i2) -> i1 + i2;
		System.out.println(INTEGER_SUM.applyAsInt(9, 3)); // = 12
		
		final LongUnaryOperator NEGATE = l -> -l;
		System.out.println(NEGATE.applyAsLong(1)); // = -1
		
		final LongBinaryOperator DIFFERENCE = (l1, l2) -> l1 - l2;
		System.out.println(DIFFERENCE.applyAsLong(1, 3)); // = -2
		
		final DoubleUnaryOperator HALF = d -> d / 2;
		System.out.println(HALF.applyAsDouble(5)); // = 2.5
		
		final DoubleBinaryOperator AVERAGE = (d1, d2) -> (d1 + d2) / 2;
		System.out.println(AVERAGE.applyAsDouble(1.0, 2.0)); // = 1.5
		
	}
	
}

Link to comment
Share on other sites

Interedasting. Read a lot about this, but haven't really ever had the need to use them.

		final IntConsumer PRINT_I = i -> System.out.println(i);
		PRINT_I.accept(3); // = 3
		
		final LongConsumer PRINT_L = l -> System.out.println(l);
		PRINT_L.accept(2); // = 2
		
		final DoubleConsumer PRINT_D = d -> System.out.println(d);
		PRINT_D.accept(2.5); // = 2.5

I reckon these could also be written as

		final IntConsumer PRINT_I = System.out::println;
		PRINT_I.accept(3); // = 3
		
		final LongConsumer PRINT_L = System.out::println;
		PRINT_L.accept(2); // = 2
		
		final DoubleConsumer PRINT_D = System.out::println;
		PRINT_D.accept(2.5); // = 2.5

For the same effect.

  • Like 1
Link to comment
Share on other sites

Interedasting. Read a lot about this, but haven't really ever had the need to use them.

		final IntConsumer PRINT_I = i -> System.out.println(i);
		PRINT_I.accept(3); // = 3
		
		final LongConsumer PRINT_L = l -> System.out.println(l);
		PRINT_L.accept(2); // = 2
		
		final DoubleConsumer PRINT_D = d -> System.out.println(d);
		PRINT_D.accept(2.5); // = 2.5

I reckon these could also be written as

		final IntConsumer PRINT_I = System.out::println;
		PRINT_I.accept(3); // = 3
		
		final LongConsumer PRINT_L = System.out::println;
		PRINT_L.accept(2); // = 2
		
		final DoubleConsumer PRINT_D = System.out::println;
		PRINT_D.accept(2.5); // = 2.5

For the same effect.

 

Shame on me sensei but I have not yet read up on method references.

It's on my TODO list for next week though :boge:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...