I have 2 Lists, the one contains a list of numbers and the other a list of names. I have prefixed the names with a number from the first list, followed by an underscore. I want to filter the second list based on all the numbers found in the first list.
What I have tried.
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()); The code above runs with no error, but the filteredList is empty. Clearly I am doing something wrong.
The filteredList should contain only:
1_John
9_Susan


Stream.of(numberList.toArray(new String[0]))=>numberList.stream(). While you're actually iterating on the incorrect list using the streams. It should be the second list to iterate on and first used to filter in.