0

I have the following list:

val tidslinjehendelser = kontaktPage.tidslinjeCard().getTidslinjehendelser()

fun getTidslinjehendelser(): MutableList<TidslinjehendelseElement> {
    return Getters.getElementsInElement(element, By.tagName("tidslinje-hendelse")).stream()
        .map { el: WebElement? -> TidslinjehendelseElement(el) }.collect(Collectors.toList())
}

Which is created from this class:

class TidslinjehendelseElement(private val element: WebElement?) {
    fun dato(): String {
    return getElementInElement(element, By.cssSelector("[data-e2e-selector=tidslinje-hendelse-dato]")).text
    }
    
    fun navn(): String { return getElementInElement(element, By.className("hendelse-navn")).text }
    
    fun innhold(): String { return getElementInElement(element, By.className("hendelse-body")).text }
}

What I want to do, is to search for an element where the innhold() text contains a certain text string.

Right now, I'm doing it with a loop:

for (hendelse in tidslinjehendelser) {
    if (hendelse.innhold().contains(melding)) {
        print("yay!")
    }
}

But since I've just started looking at streams, I'd like to explore using that method instead of the looping method.

  • Does this answer your question? [Java - Find Element in Array using Condition and Lambda](https://stackoverflow.com/questions/32262059/java-find-element-in-array-using-condition-and-lambda) – Chaosfire Feb 15 '23 at 09:39
  • 1
    Ouch, this reminds me of school, where we wrote code in French. I found it ugly, but I didn't realize how much harder this makes reading the code for people who don't understand the language. For some reason it's harder to read this than reading abstract `a`, `b`, `c` as identifiers (at least for me) – Joffrey Feb 15 '23 at 10:53

2 Answers2

1

I am writing here to find string from the list what I have understand.

List<String> result = list.stream()
  .filter(x -> x.contains("string")
  .collect(Collectors.toList());

Lets keep posted if you need more help

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
1

You can use streams to filter the tidslinjehendelser list to get only the elements whose innhold() method returns a string containing a certain text string. Here's an example:

tidslinjehendelser.stream()
.filter { hendelse -> hendelse.innhold().contains(melding) }
.forEach { hendelse -> println("yay!") }

This code uses the stream() method to convert the tidslinjehendelser list to a stream. Then, it uses the filter() method to filter the stream and keep only the elements whose innhold() method returns a string containing the melding text. Finally, it uses the forEach() method to loop over the filtered stream and print "yay!" for each matching element.

You can replace the println("yay!") statement with any other code you want to execute for the matching elements. Also note that this code will stop iterating over the stream once it finds the first matching element. If you want to find all the matching elements, you can use the collect() method instead of forEach() to collect the matching elements into a new list or other collection.

  • Thanks :-) How about if I want to put the stream inside an assertThat()? That is, assert that there is a match in on of the strings in the list? – kakemonsteret Feb 15 '23 at 10:59
  • 1
    Yes, you can use the anyMatch method of the Stream class to check if there is a match in one of the strings in the list, and then use it inside an assertThat statement to check if the condition is true. assertThat(tidslinjehendelser.stream().anyMatch { hendelse -> hendelse.innhold().contains(melding) }).isTrue() – Hafizur Rahman Arfin Feb 15 '23 at 17:45