Skip to content

Instantly share code, notes, and snippets.

@madhub
Created December 18, 2014 15:54
Show Gist options
  • Save madhub/c18cdd93b4216bf1c43f to your computer and use it in GitHub Desktop.
Save madhub/c18cdd93b4216bf1c43f to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demo;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.function.UnaryOperator;
/**
*
* @author madhub
*/
public class FunctionalInterfaceExamples {
public static void main(String[] args) {
// c# Action Interface
Supplier<String> supplier = () -> "Hello World";
supplier.get();
//
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("Hello");
// BiConsumer represents an operation that accepts two input arguments and returns no result
BiConsumer<String,String> biconsumer = (s1,s2) -> System.out.println(s1+" "+s2);
biconsumer.accept("Hello", "World");
//
Predicate<String> pre = s -> s.startsWith("H");
boolean result = pre.test("Hello");
ToIntFunction<Integer> toIntFunction = aInt -> aInt.hashCode();
Function<String,Integer> funStrToLen = s -> s.length();
int len = funStrToLen.apply("Hello World");
// BiFunction represents a function that accepts two arguments and produces a result.
BiFunction<String,String,Boolean> testEquals = (s1,s2) -> s1.equals(s2);
boolean eq = testEquals.apply("Hello", "Hello");
// UnaryOperator represents an operation on a single operand that produces a result of the same type as its operand
UnaryOperator<String> i = (x)-> x.toUpperCase();
// BinaryOperator represents an operation upon two operands of the same type, producing a result of the same type.
BinaryOperator<Integer> adder = (n1, n2) -> n1 + n2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment