In Java selenium automation when is beneficial to use List, lambda? Does it make any difference to use lambda instead of List?
List<WebElement> links = driver.findElements(By.tagName("a")); // Here is List is collection
System.out.println(links.size());
//Printing link text using for..each loop(Before Java8)
for (WebElement link : links) {
System.out.println(link.getText());
}
//Printing link text using lambda expression
links.forEach(link -> System.out.println(link.getText()));
//Processing elements using stream -> filter
long workingLinks=links.stream().filter(link->link.getAttribute("href")!=null).count();
System.out.println("Working link:" workingLinks);
CodePudding user response:
Java Streams
There are a couple of benefits in using streams in Java, as an example, the flexibility to write functions at a more abstract level which can help in writing compact functions into fewer and more readable lines of code and the ease they offer for parallelization. At it's core, the Stream API is used to process collections of objects, where the stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
A couple of Java stream features are as follows:
- A stream is not a data structure instead it takes input from the Collections, Arrays and/or I/O channels.
- Streams doesn't change the original data structure, they only provide the result as per the pipelined methods.
- Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.
Operations On Streams
Intermediate Operations:
map: The map method is used to returns a stream consisting of the results of applying the given function to the elements of this stream.
List number = Arrays.asList(2,3,4,5); List square = number.stream().map(x->x*x).collect(Collectors.toList());filter: The filter method is used to select elements as per the Predicate passed as argument.
List names = Arrays.asList("Reflection","Collection","Stream"); List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());sorted: The sorted method is used to sort the stream.
List names = Arrays.asList("Reflection","Collection","Stream"); List result = names.stream().sorted().collect(Collectors.toList());
Terminal Operations:
collect: The collect method is used to return the result of the intermediate operations performed on the stream.
List number = Arrays.asList(2,3,4,5,3); Set square = number.stream().map(x->x*x).collect(Collectors.toSet());forEach: The forEach method is used to iterate through every element of the stream.
List number = Arrays.asList(2,3,4,5); number.stream().map(x->x*x).forEach(y->System.out.println(y));reduce: The reduce method is used to reduce the elements of a stream to a single value. The reduce method takes a BinaryOperator as a parameter.
List number = Arrays.asList(2,3,4,5); int even = number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans i);Here ans variable is assigned 0 as the initial value and i is added to it.
References
You can find a couple of working examples in:
- How to print runs scored by a batsmen in a scoreboard format webelements through CSS selector using Selenium and Java
- How to extract the text iterating specific rows within a table using XPath with Selenium and Java
- Cannot cast from List<Functions.Function1<Object,String>> to List creating a list from list of WebElements using Selenium and stream() Java8
