Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
444 views
Welcome To Ask or Share your Answers For Others

1 Answer

Replace get() with orElse(null).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...