Sunday, 13 May 2018

Lambda expression that return true or false for the given input values

Define a Functional interface such that, it takes two input values and return true/false after processing the inputs.

BiObjectFunction.java
package com.sample.functional.interfaces;

/**
 * Functional interface that accepts two object and compute the result by
 * processing these two objects.
 * 
 * @author Krishna
 *
 * @param <T1>
 *            First value type
 * 
 * @param <T2>
 *            Second value type
 */
public interface BiObjectFunction<T1, T2> {

 /**
  * Calculate the value by processing two input arguments.
  * 
  * @param object1
  *            First value
  * @param object2
  *            Second value
  * 
  * @return true or false by processing arguments <code>object1</code>,
  *         <code>object2</code>
  */
 public boolean processInfo(T1 object1, T2 object2);
}

Application.java

package com.sample.app;

import com.sample.functional.interfaces.BiObjectFunction;

public class Application {

 private static boolean getResult(BiObjectFunction<Integer, Integer> biObjFunction, int data1, int data2) {
  return biObjFunction.processInfo(data1, data2);
 }

 public static void main(String[] args) {
  int val1 = 10;
  int val2 = 20;

  boolean result1 = getResult((var1, var2) -> {
   return var1 > var2;
  }, 10, 20);
  
  boolean result2 = getResult((var1, var2) -> {
   return var1 < var2;
  }, 10, 20);
  
  boolean result3 = getResult((var1, var2) -> {
   return var1 == var2;
  }, 10, 20);
  
  boolean result4 = getResult((var1, var2) -> {
   return var1 != var2;
  }, 10, 20);

  System.out.println(String.format("is %d > %d, %b", val1, val2, result1));
  System.out.println(String.format("is %d < %d, %b", val1, val2, result2));
  System.out.println(String.format("is %d == %d, %b", val1, val2, result3));
  System.out.println(String.format("is %d != %d, %b", val1, val2, result4));
 }
}


Output

is 10 > 20, false
is 10 < 20, true
is 10 == 20, false
is 10 != 20, true



Previous                                                 Next                                                 Home

No comments:

Post a Comment