Код: Выделить всё
public CustomObject getLastObject(List list) {
for (int index = list.size() - 1; index > 0; index--) {
if (list.get(index) != null) {
return list.get(index);
}
}
// handling of case when all elements are null
// or list is empty
...
}
Код: Выделить всё
public void someMethod(List list) {
.....
CustomObject object = getFirstObject(list).orElseGet(/*handle this case*/);
.....
}
public Optional getFirstObject(List list) {
return list.stream().filter(object -> object != null).findFirst();
}
Подробнее здесь: https://stackoverflow.com/questions/326 ... ing-java-8
Мобильная версия