-6

I have this expresion and I want to have it in a sigle line using the ? operator.

for (Area a : listaArea) {
    if (a.getIdArea() == user.getIdArea()) {
        user.setNomArea(a.getNomArea());
    }
}

This is what I tried:

for (Area a : listaArea) {a.getIdArea() == user.getIdArea() ? user.setNomArea(a.getNomArea())}

How to convert that if into a ? expresion?

stuXnet
  • 4,309
  • 3
  • 22
  • 31
Kaz Miller
  • 949
  • 3
  • 22
  • 40

1 Answers1

0

As the JLS states:

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

Assuming that your setter is more likely to have no return type, then no it's not possible to use the ternary operator in this context.

user2336315
  • 15,697
  • 10
  • 46
  • 64