How to write a function returns first which is not null, otherwise null [closed]

Just do a for-loop and return when a non-null object is found.

for (Object obj : objects) {
    if (obj != null) {
        return obj;
    }
}
return null;

Leave a Comment