arunkumar sambu
arunkumar sambu

Reputation: 663

How to use Java lambda Expressions for regular expressions

In below code i am getting all elements from the list and splitting the text after "(".

for (WebElement element : measureKeys) {
    String[] measure = element.getText().split("\\(");
    testing.add(measure[0])
}

How to use lambdas for above code

Upvotes: 1

Views: 2601

Answers (3)

Ashlin Karkada
Ashlin Karkada

Reputation: 1410

Another alternative is ,

List<String> list= Pattern.compile("\\(")
                      .splitAsStream(measureKeys)
                      .collect(Collectors.toList());

Upvotes: 1

J.Joe
J.Joe

Reputation: 81

    List<String> measureKeys = new ArrayList<>();
    List<String> a = new ArrayList<>();
    measureKeys.forEach(element -> a.add(element.split("\\(")[0]));

Is that your wish ?

Upvotes: 0

Mureinik
Mureinik

Reputation: 311893

You could essentially turn the body of the loop to a lambda expression and collect the the results

List<String> testing = 
    measureKeys.stream()
               .map(e -> element.getText().split("\\(")[0])
               .collect(Collectors.toList());

Upvotes: 8

Related Questions