如何使用 Java 8 流和从值数组开始过滤列表
How to filter a List using Java 8 stream and startwith array of values
我有 2 个列表,一个包含数字列表,另一个包含名称列表。我在名称前面加上第一个列表中的数字,然后是下划线。我想根据第一个列表中找到的所有数字过滤第二个列表。
我试过的。
List<String> numberList = new ArrayList<>();
numberList.add("1_");
numberList.add("9_");
List<String> nameList = new ArrayList<>();
nameList.add("1_John");
nameList.add("2_Peter");
nameList.add("9_Susan");
List<String> filteredList = Stream.of(numberList.toArray(new String[0]))
.filter(str -> nameList.stream().anyMatch(str::startsWith))
.collect(Collectors.toList());
上面的代码运行没有错误,但是 filteredList 是空的。很明显我做错了什么。
过滤列表应仅包含:
1_John
9_Susan
您在错误的 String
上调用了 startsWith
(例如,您测试的是 "1_".startsWith("1_John")
而不是 "1_John".startsWith("1_")
)。
您应该流过 nameList
并使用 numberList
进行过滤:
List<String> filteredList =
nameList.stream()
.filter(str -> numberList.stream().anyMatch(str::startsWith))
.collect(Collectors.toList());
P.S。 Stream.of(numberList.toArray(new String[0]))
是多余的。请改用 numberList.stream()
。
作为 Eran 解决方案的替代方案,您还可以使用 removeIf
和 noneMatch
的组合,如下所示:
List<String> filteredList = new ArrayList<>(nameList);
filteredList.removeIf(str -> numberList.stream().noneMatch(str::startsWith));
另一种尝试提供解决方案的方法是使用 contains
,尽管 startsWith
和 contains
的实际实现不同,但在 OP 情况下两者都会获取 相同 结果。
List<String> strList = nameList.stream().filter(s->numberList.stream().anyMatch(s::contains)).collect(Collectors.toList());
输出:
[1_John, 9_Susan]
List<String> numberList = new ArrayList<>();
numberList.add("1_");
numberList.add("9_");
List<String> nameList = new ArrayList<>();
nameList.add("1_John");
nameList.add("2_Peter");
nameList.add("9_Susan");
正确代码如下:
您的代码使用了以下内容:
我有 2 个列表,一个包含数字列表,另一个包含名称列表。我在名称前面加上第一个列表中的数字,然后是下划线。我想根据第一个列表中找到的所有数字过滤第二个列表。
我试过的。
List<String> numberList = new ArrayList<>();
numberList.add("1_");
numberList.add("9_");
List<String> nameList = new ArrayList<>();
nameList.add("1_John");
nameList.add("2_Peter");
nameList.add("9_Susan");
List<String> filteredList = Stream.of(numberList.toArray(new String[0]))
.filter(str -> nameList.stream().anyMatch(str::startsWith))
.collect(Collectors.toList());
上面的代码运行没有错误,但是 filteredList 是空的。很明显我做错了什么。
过滤列表应仅包含:
1_John
9_Susan
您在错误的 String
上调用了 startsWith
(例如,您测试的是 "1_".startsWith("1_John")
而不是 "1_John".startsWith("1_")
)。
您应该流过 nameList
并使用 numberList
进行过滤:
List<String> filteredList =
nameList.stream()
.filter(str -> numberList.stream().anyMatch(str::startsWith))
.collect(Collectors.toList());
P.S。 Stream.of(numberList.toArray(new String[0]))
是多余的。请改用 numberList.stream()
。
作为 Eran 解决方案的替代方案,您还可以使用 removeIf
和 noneMatch
的组合,如下所示:
List<String> filteredList = new ArrayList<>(nameList);
filteredList.removeIf(str -> numberList.stream().noneMatch(str::startsWith));
另一种尝试提供解决方案的方法是使用 contains
,尽管 startsWith
和 contains
的实际实现不同,但在 OP 情况下两者都会获取 相同 结果。
List<String> strList = nameList.stream().filter(s->numberList.stream().anyMatch(s::contains)).collect(Collectors.toList());
输出:
[1_John, 9_Susan]
List<String> numberList = new ArrayList<>();
numberList.add("1_");
numberList.add("9_");
List<String> nameList = new ArrayList<>();
nameList.add("1_John");
nameList.add("2_Peter");
nameList.add("9_Susan");
正确代码如下:
您的代码使用了以下内容: