I have the following task.
We have two lists: the first one integerList of type Integer and the second stringList of type String.
The goal is:
- For each value
nfromintegerListselect a string fromstringListthat starts with a digit and has a lengthn; - If there are several required strings in the
stringList, select the first one; - If there's no required string, place
"Not found"as an appropriate element.
My code:
public static List<String> foo(List<Integer> integerList, List<String> stringList) {
return integerList.stream()
.map(integer -> stringList.stream()
.filter(...) // not sure what to use here
.findFirst()
.orElse("Not found"))
.collect(Collectors.toList());
}
I'm not sure what should I use in the filter.
Input:
integerList = [1, 3, 4]
stringList = ["1aa", "aaa", "1", "a"]
Desired output:
["1", "1aa", "Not Found"]