How to check if element exists using a lambda expression?

前端 未结 3 976
失恋的感觉
失恋的感觉 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:43

    The above answers require you to malloc a new stream object.

    public 
    boolean containsByLambda(Collection c, Predicate p) {
    
        for (final T z : c) {
            if (p.test(z)) {
                return true;
            }
        }
        return false;
    }
    
    public boolean containsTabById(TabPane tabPane, String id) {
        return containsByLambda(tabPane.getTabs(), z -> z.getId().equals(id));
    }
    ...
    if (containsTabById(tabPane, idToCheck))) {
       ...
    }
    

提交回复
热议问题