Sunday 13 May 2018

Lambda expression that returns a Boolean value

Define a Functional interface that returns a boolean value.

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

/**
 * Functional interface returns a boolean value.
 * 
 * @author krishna
 *
 */
public interface BooleanProvider {

 /**
  * 
  * @return a boolean value
  */
 public boolean getAsBoolean();
}

Application.java

package com.sample.app;

import com.sample.functional.interfaces.BooleanProvider;

public class Application {
 
 private static void processInfo(BooleanProvider boolProvider) {
  if(boolProvider.getAsBoolean()) {
   System.out.println("Provider is returning true");
   return;
  }
  
  System.out.println("Provider returning false");
 }

 
 public static void main(String[] args) {
  processInfo(() -> true);
  processInfo(() -> false);
  
 }
}


Output

Provider is returning true
Provider returning false




Previous                                                 Next                                                 Home

No comments:

Post a Comment