Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Code Block
people << new Person(name:'Harry')
biggestSalary = people.collect{ p -> p.job?.salary }.max()
println biggestSalary

Two things are going on hear here to allow this to work. First of all functions like , max() are is 'null aware' so that 300, null, 400.max() == 400 and secondly with the ?. operator, an expression like p?.job?.salary will be equal to null if salary is equal to null, or if job is equal to null or if p is equal to null. You don't need to code a complex nested if ... then ... else to avoid a NullPointerException.