【问题标题】:How do i combine condition statements with this particular lambda expression?如何将条件语句与这个特定的 lambda 表达式结合起来?
【发布时间】:2019-04-17 09:34:09
【问题描述】:

我有一个使用 lambda 表达式编写的 java 方法,我需要添加更多条件,并且我被禁止使用经典的 if 和 elses。这是我当前的代码,用于检查 getInstrument 是否具有正确的 ENUM:

public static Predicate<Deal> isDeal() {
  return  i ->
    i.getInstrument() == ENUM1
      || i.getInstrument() == ENUM2
      || i.getInstrument() == ENUM3;
}

对于这段代码,我需要添加一个条件来检查i.getGroup() 是否为空,然后继续检查枚举是否正确。我还需要添加一个条件,如果 i.getGroup() != null and i.getGroup() != "NODEAL"i.getInstrument() 不是 ENUM2 或 ENUM3,它返回 i。这就是我用经典 if 和 else 编写它的方式:

public static Predicate<Deal> is Deal() {

    if ( i.getGroup() == null && i.getInstrument() == ENUM1
                || i.getInstrument() == ENUM2
                || i.getInstrument() == ENUM3) {
      return i;
    } else if ( i.getGroup() != null && i.getGroup() == "DEAL" && 
    i.getInstrument() != ENUM2 || i.getInstrument() != ENUM3) {
      return i;
    }
}

我怎么能用 lambda 写这个?

【问题讨论】:

  • 提示:经典的 if 需要在 && 之后加上括号,否则将无法按预期工作
  • 为什么不允许使用“经典的 if 和 elses”?而且,请确保它符合要求,现在 Predicate&lt;Deal&gt; is Deal() 没有什么意义 - i 绝对不是 Predicate
  • @AndrewTobilko 我以为这是家庭作业
  • @AndrewTobilko 因为客户希望它带有 Lambda 表达式。
  • 在第二个代码中,您缺少返回语句。

标签: java if-statement lambda java-8 predicate


【解决方案1】:

类似

Predicate<Deal> isDeal() {
        return deal -> deal.getGroup() == null 
                       && EnumSet.of(Instrument.ENUM1, 
                                     Instrument.ENUM2,
                                     Instrument.ENUM3)
                                 .contains(deal.getInstrument())
                ||
                "DEAL".equals(deal.getGroup()) 
                && !EnumSet.of(Instrument.ENUM2,
                                     Instrument.ENUM3)
                                 .contains(deal.getInstrument())
}

【讨论】:

    【解决方案2】:

    类似的东西

    public static Predicate<Deal> isDeal() {
       return  i->
            (i.getGroup() == null 
            && (i.getInstrument() == ENUM1
                || i.getInstrument() == ENUM2
                || i.getInstrument() == ENUM3)
            )
            ||
            ( i.getGroup() != null 
              && i.getGroup() == "DEAL" 
              && i.getInstrument() != ENUM2 && i.getInstrument() != ENUM3);
    }
    

    PS:请记住,您应该将字符串与equals 而非== 进行比较。

    【讨论】:

      【解决方案3】:

      我不知道您的业务领域,但我试图使条件更具表现力、可读性和可维护性。我还会问客户为什么他们想要使用 lambda,因为我认为没有理由在这里使用它们。一个if-else 语句将给你同样的没有开销。

      public Predicate<Deal> isDeal() {
          return isGroupAbsent().or(isGroupPresent());
      }
      
      private Predicate<Deal> isGroupPresent() {
          Instrument i = i.getInstrument();
          return d -> "DEAL".equals(d.getGroup()) && i != ENUM2 && i != ENUM3;
      }
      
      private Predicate<Deal> isGroupAbsent() {
          Instrument i = i.getInstrument();
          return d -> d.getGroup() == null && (i == ENUM1 || i == ENUM2 || i == ENUM3);
      }
      

      【讨论】:

        【解决方案4】:

        这是ternary operator ? : 的完美工作

        但是你的最后一个条件还是有问题,它应该返回什么,truefalse ?知道Predicate&lt;T&gt;签名是boolean test(T t);

        public static Predicate<Deal> isDeal() {
            return  i-> i.getGroup() == null ? 
                            EnumSet.of(ENUM1, ENUM2, ENUM3).contains(i.getInstrument()) : 
                            !"DEAL".equals(i.getGroup()) ? 
                                !EnumSet.of(ENUM2, ENUM3).contains(i.getInstrument()) : 
                                false; // What do you put here? This is missing in your question  
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-12
          • 2013-12-14
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          • 2012-06-03
          • 1970-01-01
          相关资源
          最近更新 更多