Let's say I have a set of cars, where each car has a steering wheel. I'd like to write a line of code that looks for a car in the set and returns its steering wheel, or returns null if the car isn't in the set. Something like this:
Car found = // either a Car or null
SteeringWheel wheel = (found == null ? null : found.steeringwheel);
Is there a way to do this without using found
and null
twice in the expression? I don't like the smell of the repetition here.