将两个列表与一个列表进行比较 JAVA 流不起作用
Compare two lists with one JAVA Stream do not work
我有一个任务:
A sequence of positive integers numbers and a sequence of strings stringList are given. Get a new sequence of strings according to the following rule: for each value n from
sequence numbers, select a string from sequence stringList that starts with a digit and has length n.
If there are several required strings in the stringList sequence, return the first; if there are none,then return the string "Not found"
例如:
input: {1, 3, 4}, {"1aa", "aaa", "1", "a"}
output: {"1", "1aa", "Not Found"}
我的输出:
[java.util.stream.ReferencePipeline@7f690630,
java.util.stream.ReferencePipeline@edf4efb,
java.util.stream.ReferencePipeline@2f7a2457]
我的代码:
(List<Integer> numbers, List<String> stringList) {
return numbers.stream()
.map(Object::toString)
.map(value -> (stringList.stream().filter(e -> (Character.isDigit(e.charAt(0)))).map(s -> {
if (((Object) (s.length())).toString().equals(value)) return s;
return "Not Found";
}))).map(Object::toString).collect(Collectors.toList());
请帮忙。
以下代码使用正则表达式 "\d.*"
来匹配以数字开头的字符串应该有效。
此外,无需将整数值转换为字符串,然后再将此字符串转换回 int 以匹配长度。
public static List<String> task(List<Integer> nums, List<String> strs) {
return nums.stream()
.map(i -> strs.stream()
//.filter(s -> s.length() == i && i > 0 && Character.isDigit(s.charAt(0)))
.filter(s -> s.length() == i && s.matches("\d.*"))
.findFirst() // Optional<String>
.orElse("Not Found")
)
.collect(Collectors.toList());
}
测试
System.out.println(task(Arrays.asList(1, 3, 4), Arrays.asList("1aa", "aaa", "1", "a")));
输出:
[1, 1aa, Not Found]
更新
要摆脱嵌套流,应将字符串列表转换为过滤映射,其中长度是键,第一个匹配字符串是值,然后在流式传输整数列表时使用此映射:
public static List<String> taskFast(List<Integer> nums, List<String> strs) {
Map<Integer, String> strByLength = strs
.stream()
.filter(s -> s.length() > 0 && Character.isDigit(s.charAt(0)))
.collect(Collectors.toMap(
String::length,
s -> s,
(s1, s2) -> s1 // keep the first match for the same length
));
return nums.stream()
.map(i -> strByLength.getOrDefault(i, "Not Found"))
.collect(Collectors.toList());
}
我有一个任务:
A sequence of positive integers numbers and a sequence of strings stringList are given. Get a new sequence of strings according to the following rule: for each value n from sequence numbers, select a string from sequence stringList that starts with a digit and has length n.
If there are several required strings in the stringList sequence, return the first; if there are none,then return the string "Not found"
例如:
input: {1, 3, 4}, {"1aa", "aaa", "1", "a"}
output: {"1", "1aa", "Not Found"}
我的输出:
[java.util.stream.ReferencePipeline@7f690630,
java.util.stream.ReferencePipeline@edf4efb,
java.util.stream.ReferencePipeline@2f7a2457]
我的代码:
(List<Integer> numbers, List<String> stringList) {
return numbers.stream()
.map(Object::toString)
.map(value -> (stringList.stream().filter(e -> (Character.isDigit(e.charAt(0)))).map(s -> {
if (((Object) (s.length())).toString().equals(value)) return s;
return "Not Found";
}))).map(Object::toString).collect(Collectors.toList());
请帮忙。
以下代码使用正则表达式 "\d.*"
来匹配以数字开头的字符串应该有效。
此外,无需将整数值转换为字符串,然后再将此字符串转换回 int 以匹配长度。
public static List<String> task(List<Integer> nums, List<String> strs) {
return nums.stream()
.map(i -> strs.stream()
//.filter(s -> s.length() == i && i > 0 && Character.isDigit(s.charAt(0)))
.filter(s -> s.length() == i && s.matches("\d.*"))
.findFirst() // Optional<String>
.orElse("Not Found")
)
.collect(Collectors.toList());
}
测试
System.out.println(task(Arrays.asList(1, 3, 4), Arrays.asList("1aa", "aaa", "1", "a")));
输出:
[1, 1aa, Not Found]
更新
要摆脱嵌套流,应将字符串列表转换为过滤映射,其中长度是键,第一个匹配字符串是值,然后在流式传输整数列表时使用此映射:
public static List<String> taskFast(List<Integer> nums, List<String> strs) {
Map<Integer, String> strByLength = strs
.stream()
.filter(s -> s.length() > 0 && Character.isDigit(s.charAt(0)))
.collect(Collectors.toMap(
String::length,
s -> s,
(s1, s2) -> s1 // keep the first match for the same length
));
return nums.stream()
.map(i -> strByLength.getOrDefault(i, "Not Found"))
.collect(Collectors.toList());
}