October 1, 2022

UnaryOperator Functional Interface Java Examples

In this post we’ll see examples of Java java.util.function.UnaryOperator functional interface.

UnaryOperator functional interface represents an operation on a single operand returning a result of the same type as its operand. UnaryOperator extends java.util.function.Function interface and provides behavior for the case where the operand and result are of the same type. Since it extends Function so inherits all the methods of the Function interface-

  • andThen(Function<? super R, ? extends V> after)- It is a default interface method which takes another Function as argument and returns a composed Function that performs, in sequence, first the operation of the calling Function followed by the after operation.
  • compose(Function<? super V, ? extends T> before)- It is a default method in Function interface which takes another Function as argument and returns a composed Function that performs, in sequence, first the before operation then the operation of the calling Function.
  • R apply(T t)- Here T is the type of the argument passed to the method and it returns a value of type R. This is the abstract method in this functional interface. If you are writing a Lambda expression that takes single argument of one type and returns a value of the same type then that lambda expression can be written as an implementation of UnaryOperator built-in functional interface where lambda expression implements the apply() method.

identity() method is changed to return the UnaryOperator-

  • identity()- It is a static method that returns a UnaryOperator which returns its input argument.

UnaryOperator interface apply() method example

In the example apply method is implemented as a lambda expression that returns the number squared. Both the passed argument and the return value are of type int.

import java.util.function.UnaryOperator;

public class UnaryOperatorExample {
  public static void main(String[] args) {
    UnaryOperator unaryOperator = (n) -> n*n;
    System.out.println("3 squared is- " + unaryOperator.apply(3));
    System.out.println("9 squared is- " + unaryOperator.apply(9));
  }
}
Output
3 squared is- 9
9 squared is- 81

UnaryOperator functional interface andThen() method example

If you want to get a number squared and then add another integer to it that can be done as a sequence of operation using andThen() method.

import java.util.function.UnaryOperator;

public class UnaryOperatorExample {
  public static void main(String[] args) {
    UnaryOperator<Integer> unaryOperator1 = (n) -> n*n;
    UnaryOperator<Integer> unaryOperator2 = (n) -> n + 1;
    System.out.println("Result- " + unaryOperator1.andThen(unaryOperator2).apply(3));
    System.out.println("Result- " + unaryOperator1.andThen(unaryOperator2).apply(9));
  }
}
Output
Result- 10
Result- 82

UnaryOperator functional interface compose() method example

compose() method does the reverse of andThen() method so if we take the previous example and add an integer first and then square it then that sequence can be composed using compose() method.

import java.util.function.UnaryOperator;

public class UnaryOperatorExample {
  public static void main(String[] args) {
    UnaryOperator<Integer> unaryOperator1 = (n) -> n*n;
    UnaryOperator<Integer> unaryOperator2 = (n) -> n + 1;
    System.out.println("Result- " + unaryOperator1.compose(unaryOperator2).apply(3));
    System.out.println("Result- " + unaryOperator1.compose(unaryOperator2).apply(9));
  }
}
Output
Result- 16
Result- 100

That's all for the topic UnaryOperator Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment