Reputation: 155
Suppose I have the following code:
public int getNumOfPostInstancesByTitle(String postMainTitle) {
int numOfIns = 0;
List<WebElement> blogTitlesList = driver.findElements(blogTitleLocator);
for (WebElement thisBlogTitle : blogTitlesList) {
String currentTitle = thisBlogTitle.getText();
if (currentTitle.equalsIgnoreCase(postMainTitle)) {
numOfIns++;
}
}
return numOfIns;
}
what is the proper way converting it with predicate lambda?
Upvotes: 3
Views: 328
Reputation: 394146
You can calculate your numOfInts
with a simple combination of map
, filter
and count
:
return driver.findElements(blogTitleLocator)
.stream()
.map(WebElement::getText) // convert to a Stream of String
.filter(s -> s.equalsIgnoreCase(postMainTitle)) // accept only Strings
//equal to postMainTitle
.count(); // count the elements of the Stream that passed the filter
Upvotes: 5