I'm struggling a bit with a pretty simple case of mapping a list of Selenium WebElements to a List of classes using Java Streams.
The element:
val elements: List<WebElement> = element.findElements(By.cssSelector("[data-e2e-selector=expense]"))
(Currently, there are only two elements in the list.)
I'm currently mapping the WebElement list to a MutableList of classes the "classic way":
val expenseElements: MutableList<ExpenseElement> = ArrayList()
for (element in elements) {
expenseElements = ExpenseElement(element)
}
Which is working fine. But I feel I should learn to use Stram() to do this more "elegantly". Or, at least, because I want to :-)
But my feeble attempt at using Stream()
val expenseElements = elements.stream().map { UtgiftElement(element) }.collect(Collectors.toList())
results in only the first element being included. So the first elements takes up both places in the the list.
The first method:
expenseElements[0].expense() = "Entry 1"
expenseElements[1].expense() = "Entry 2"
The non-working Stream() method:
expenseElements[0].expense() = "Entry 1"
expenseElements[1].expense() = "Entry 1"
This is basic I know, but I've tried replicating code that does the same in Java. I have to start somewhere.
CodePudding user response:
There's a mistake in your mapping lambda. You do:
map { UtgiftElement(element) }
But that is not correct. The map function provides you with a single input, named it
by default. Your lambda should either by map { UtgiftElement(it) }
or map { element -> UtgiftElement(element) }
.
I do not know where your element variable comes from, but I think it is not one of the elements in the stream.