Warm‑up: stream() + filter/map/collect (with answer)
Warm‑up: Given a list of prices as strings (may contain spaces and currency symbols), create a stream pipeline that: 1) trims each value 2) removes currency symbols like ₹, $ 3) converts to Integer 4) keeps only values >= 100 5) collects to List<Integer> Implement ONLY: warmUp() to print the final list.
import java.util.*;
import java.util.stream.*;
public class Main {
// ✅ TODO: Student must implement only this method
static void warmUp() {
// TODO:
// - create list of strings
// - build stream pipeline: trim -> remove symbols -> parse -> filter >=100 -> collect
// - print the result list
throw new UnsupportedOperationException("TODO");
}
public static void main(String[] args) {
warmUp();
}
}
Answer (Logic + Code)
Logic: - stream(): processes a collection in a pipeline - map(): transforms each element - filter(): keeps elements that match a condition - collect(toList()): turns stream back into a List
static void warmUp() {
List<String> prices = Arrays.asList(" ₹99 ", " $120 ", " 250✅ ", " 80 ", " ₹150 ");
List<Integer> out = prices.stream()
.map(s -> s.trim()) // remove surrounding spaces
.map(s -> s.replaceAll("[^0-9]", "")) // keep digits only
.filter(s -> !s.isEmpty()) // safety
.map(Integer::parseInt) // to integer
.filter(v -> v >= 100) // business rule
.collect(Collectors.toList()); // back to list
System.out.println(out);
}