How to check if element exists using a lambda expression?

前端 未结 3 969
失恋的感觉
失恋的感觉 2020-12-07 15:26

Specifically, I have TabPane, and I would like to know if there is element with specific ID in it.

So, I would like to do this with lambda expression in Java:

<
3条回答
  •  情歌与酒
    2020-12-07 15:54

    While the accepted answer is correct, I'll add a more elegant version (in my opinion):

    boolean idExists = tabPane.getTabs().stream()
        .map(Tab::getId)
        .anyMatch(idToCheck::equals);
    

    Don't neglect using Stream#map() which allows to flatten the data structure before applying the Predicate.

提交回复
热议问题