3

I need to know following code can be convert to lamda expression.

private class K<D extends BaseDose<I>, I extends DoseInstance>
    implements Predicate<Prescription<D, I>>
  {
    @Override
    public boolean test(final Prescription<D, I> input)
    {
      return DepositType.DATE == input.getBaseDeposit().getDepositType();
    }
  }
2
  • 3
    Predicate<Prescription<D, I>> is a functional interface, so yes. The whole thing can probably be written as input -> DepositType.DATE == input.getBaseDeposit().getDepositType(). Commented Jul 16, 2019 at 6:48
  • 2
    @ernest_k Shouldn't this be an answer? Commented Jul 16, 2019 at 6:54

1 Answer 1

4

Instead of creating a class (your K class) that implements Predicate<Prescription<D, I>>, you can assign a lambda expression to a Predicate variable.

For example:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> DepositType.DATE == p.getBaseDeposit().getDepositType();

If you need a lambda expression with multiple statements, you can write:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> {
        final SigningStatus signingStatus = p.getSigningStatus();
        return !p.isReplacedByLatest() && SigningStatus.INVALIDATED.equals(signingStatus);
    };

though this can be simplified to a single statement:

Predicate<Prescription<BaseDose<DoseInstance>, DoseInstance>> pred =
    p -> !p.isReplacedByLatest() && SigningStatus.INVALIDATED.equals(p.getSigningStatus());
Sign up to request clarification or add additional context in comments.

2 Comments

For the OP : Predicate<Prescription<?>> should also work if Prescription is bounded with BaseDose and DoseInstance, which would make sense.
@Eran 'inside test , like this code ' final SigningStatus signingStatus = input.getSigningStatus(); return !input.isReplacedByLatest() && SigningStatus.INVALIDATED.equals(signingStatus);can aslo convert to lambda?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.