[英]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 如何为上述代码使用Lambda
You could essentially turn the body of the loop to a lambda expression and collect the the results 您基本上可以将循环的主体转换为lambda表达式并收集结果
List<String> testing =
measureKeys.stream()
.map(e -> element.getText().split("\\(")[0])
.collect(Collectors.toList());
Another alternative is , 另一种选择是
List<String> list= Pattern.compile("\\(")
.splitAsStream(measureKeys)
.collect(Collectors.toList());
List<String> measureKeys = new ArrayList<>();
List<String> a = new ArrayList<>();
measureKeys.forEach(element -> a.add(element.split("\\(")[0]));
Is that your wish ? 那是你的愿望吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.