'Optional.get()' without 'isPresent()' check

Dims picture Dims · Aug 2, 2016 · Viewed 83.4k times · Source

I have the following search code in Java:

return getTableViewController().getMe().getColumns().stream().filter($->Database.equalsColumnName($.getId(), columnId)).findFirst().get();

I was wishing to find column by name and return first one found.

I understand there is a case when nothing found and it should be processed, but how?

Is this what it wants by this swearing:

'Optional.get()' without 'isPresent()' check

?

How to fix? I wish to return null if nothing found.

UPDATE

Okay, okay, I just didn't realize, that findFirst() returns Optional.

Answer

Andy Turner picture Andy Turner · Aug 2, 2016

Replace get() with orElse(null).