Java Stream How to - Create Predicate lambda








Question

We would like to know how to create Predicate lambda.

Answer

/*from w ww  .j av a 2s.c o m*/
import java.util.function.Predicate;

public class Main {
  static Predicate<Trade> p = t -> t.getQuantity() > 100000;
  private static void testBigTrade(Trade t) {
    if (p.test(t))
      System.out.println("Big Trade! " + t);
    else
      System.out.println("Nah, not a big Trade! " + t);
  }

  public static void main(String[] args) {
    testBigTrade(new Trade(1, "XT", 20000000, "NEW"));
    testBigTrade(new Trade(1, "XT", 10000, "NEW"));
  }
}


class Trade {

  private int tradeId = 0;
  private String issuer = null;
  private int quantity = 0;
  private String status = null;

  public Trade(int id, String issuer, int quantity, String status) {
    this.tradeId = id;
    this.issuer = issuer;
    this.quantity = quantity;
    this.status = status;
  }

  public int getTradeId() {
    return tradeId;
  }

  public void setTradeId(int tradeId) {
    this.tradeId = tradeId;
  }

  public String getIssuer() {
    return issuer;
  }

  public void setIssuer(String issuer) {
    this.issuer = issuer;
  }

  public int getQuantity() {
    return quantity;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  @Override
  public String toString() {
    return "Trade{" + "tradeId=" + tradeId + ", issuer=" + issuer
        + ", quantity=" + quantity + ", status=" + status + '}';
  }

}

The code above generates the following result.