Skip to main content

Posts

Showing posts with the label Function Types

Java 8 - Functions - Function Types - Supplier

A Supplier is a function which returns an output without any input.
@Slf4j
public class Supplier_ {

    static Supplier<Person> personSupplier = () -> new Person("Tom", "Smith");

    public static void main(String[] args) {
      log.info("supplied person is " + personSupplier.get());
    }

}

Java 8 - Functions - Function Types - Predicate

A Predicate is a function which acts on an input to return a boolean.A Predicate can be transformed to its negated value using negatePredicates can be composed using and and or.
@Slf4j
public class Predicate_ {

   static Function<String, Boolean> normalPredicateFunc = s -> s.length() > 0;
   static Predicate<String> nonEmptyString = s -> s.length() > 0;

   static Predicate<String> emptyString = nonEmptyString.negate();

   static Predicate<String> correctLength = nonEmptyString.and(s -> s.length() > 2);
   static Predicate<String> validString = correctLength.or(nonEmptyString);

   public static void main(String[] args) {
     log.info("predicates match: " + (nonEmptyString.test("hi") == normalPredicateFunc.apply("hi")));

     log.info("string is empty: " + emptyString.test(""));

     log.info("string is valid: "…

Java 8 - Functions - Function Types - Identity Function

An Identity function is a function which returns it's input as output.

@Slf4j
public class IdentityFunction {

   static Function<Integer, Integer> identifyFunc = x -> x;
   static Function<Integer, Integer> identifyFunc2 = Function.identity();
   static Function<Integer, Integer> identifyFunc3 = UnaryOperator.identity();

   public static void main(String[] args) {
     log.info("identify function input matches output: " + (identifyFunc.apply(1) == 1));
     log.info("second identify function input matches output: " + (identifyFunc2.apply(2) == 2));
     log.info("third identity function input matches output: " + (identifyFunc3.apply(3) == 3));
   }
}


Java 8 - Functions - Function Types - Primitive Args Functions

There are cases when the performance cost of auto boxing is significant.When dealing with a lot of primitive values, avoid auto boxing by creating lambdas that use primitives.The examples shown below use int primitive type, but there are corresponding versions for the double and long primitive types, just replace Int with Double or Long.
@Slf4j
public class FunctionsWithPrimitiveArgs {

    /*
     function to convert int to any type
    */
    IntFunction<String> intToString = i -> Integer.toString(i);

    /*
     function to convert any type to int
    */
    ToIntFunction<String> parseInt = str -> Integer.valueOf(str);

    /*
     function that takes an int to return a boolean i.e a Predicate accepting int input
    */
    IntPredicate isEven = i -> i % 2 == 0;

    /*
     function that takes two inputs to return an int
    */
    ToIntBiFunction<String,String> maxLength = (left, right…

Java 8 - Functions - Function Types - Function

A Function acts on an input and returns an output.A UnaryOperator is a function whose input and output are of the same type.A BiFunction is a function which acts on two inputs and returns an output.A BinaryOperator is a BiFunction whose input and output are of the same type.BinaryOperator.minBy and BinaryOperator.maxBy return a BinaryOperator which calculate min and max values of two inputs.
@Slf4j
public class Function_ {

   static Function<Integer, Integer> incrementFunction = input -> input+1;
   static UnaryOperator<Integer> incrementFunction_2 = input -> input+1;

   static BiFunction<String, Integer, String> biFunction = (a, b) -> a + b;
   static BinaryOperator<String> concatFunction = (a, b) -> a + b;

   static BinaryOperator<Integer> max = BinaryOperator.maxBy((a,b) -> a.compareTo(b));
   static BinaryOperator<Integer> min = BinaryOperator.minBy((a,b) -> a-b);

   public static void main(Strin…

Java 8 - Functions - Function Types - Consumer

A Consumer is a function which takes an input and returns no output.A BiConsumer is a Consumer which accepts two inputs.
@Slf4j
public class Consumer_ {

   static Consumer<Person> greeter = person -> log.info("Hello, " + person.getFirstName());

   static Consumer<Iterable> printObjects = list -> list.forEach(item -> log.info(item.toString()));

   static BiConsumer<String, String> stringBiConsumer = (input1, input2) -> log.info(input1 + " " + input2);

   public static void main(String[] args) {
     greeter.accept(new Person("Tom", "Smith"));
     printObjects.accept(Arrays.asList("one", "two", "three"));
     stringBiConsumer.accept("log1", "log2");
   }

}

Java 8 - Functions - Function Types - Comparator

The Comparator function implements the Comparator interface, which is a Functional Interface.
@Slf4j
public class Comparator_ {

   static Comparator<Person> orderByFirstNameAsc = (p1, p2) -> p1.getFirstName().compareTo(p2.getFirstName());
   static Comparator<Person> orderByFirstNameDesc = (p1, p2) -> p2.getFirstName().compareTo(p1.getFirstName());

   public static void main(String[] args) {
     Person person1 = new Person("adam", "smith");
     Person person1_clone = new Person("adam", "smith");
     Person person2 = new Person("paul", "robins");

     log.info("adam before paul in asc order: " + (orderByFirstNameAsc.compare(person1, person2) < 0));

     log.info("adam has same position with adam: " + (orderByFirstNameAsc.compare(person1, person1_clone) == 0));

    …