10. Modern Java: Streams, Lambdas & Optional for QA

Streams & Lambdas for Test Automation

import java.util.*;
import java.util.stream.Collectors;

List<String> rawTexts = Arrays.asList(" Product A ", " Product B", " ");

// Clean and filter empty items in one pipeline:
List<String> cleanTitles = rawTexts.stream()
.map(String::trim)
.filter(t -> !t.isEmpty())
.collect(Collectors.toList());

// Find first matching element safely with Optional:
Optional<String> match = cleanTitles.stream()
.filter(t -> t.contains("Product A"))
.findFirst();

if (match.isPresent()) {
System.out.println("Found target product: " + match.get());
}

Task

  • Implement getPassingTests:
  • 1. Accept List<String> testResults (e.g. ["testLogin:PASS", "testCheckout:FAIL", "testSearch:PASS"]).
  • 2. Use Streams to filter only strings that endWith(":PASS").
  • 3. Collect and return as a List<String>.
πŸ’‘Common doubts
Did you like the problem?
Lesson 10 of 38
Java
Loading code editor…
Next