Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stream on method that return boolean value with condition

I am using this method:

public boolean checkRowsFilterNameBy(String filter){
    waitForElmContainsText(searchButton, "Search");
    List<AuditRow> listRows = auditTable.getTable();
    for(AuditRow row : listRows){
        if(!row.nameStr.equals(filter)||!row.nameStr.contains(filter))
            return false;
    }
    return true;
}

and I want to be able to change it using Stream , I've tried the following, but I am missing something:

listRows.stream().forEach(auditRow -> {
           if(auditRow.actionStr.equals(filter))return true;} else return false;);

but I am getting an error.

like image 847
tupac shakur Avatar asked Feb 19 '19 17:02

tupac shakur


People also ask

Which stream API methods used to evaluate Boolean conditions and return the result?

match() Various matching operations can be used to check whether a given predicate matches the stream elements. All of these matching operations are terminal and return a boolean result.

What type of method returns a Boolean value?

Java Boolean equals() method The equals() method of Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object, else it returns false.

Is method used to return Boolean value?

valueOf() method is used to return a boolean value either “true” or “false” depending upon the value of the specified boolean object.

How do I return a Boolean in Java 8?

The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true" . Example: Boolean. parseBoolean("True") returns true . Example: Boolean.


1 Answers

You may do it like so,

listRows.stream().allMatch(row -> row.nameStr.equals(filter) && row.nameStr.contains(filter));

Update

As per Holgers suggestion, this can be further simplified as this.

listRows.stream().allMatch(row -> row.nameStr.contains(filter));

The use of equals or contains may vary depending on your context.

like image 78
Ravindra Ranwala Avatar answered Sep 28 '22 13:09

Ravindra Ranwala