我有以下任务.
我们有two lists个:第一个是Integer
类型的integerList
,第二个是String
类型的stringList
.
目标是:
- 对于
integerList
中的每个值n
,从stringList
中 Select 一个以digit开头、长度为n
的字符串; - 如果
stringList
个字符串中有多个必填字符串,则 Select 第一个; - 如果没有必需的字符串,则将
"Not found"
作为适当的元素.
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());
}
我不知道在filter
米中我应该用什么.
Input:个
integerList = [1, 3, 4]
stringList = ["1aa", "aaa", "1", "a"]
Desired output:个
["1", "1aa", "Not Found"]