我有以下简单方法:
public void exercise2(List<String> list) {
list.stream().filter(s -> s.length() % 2 == 0).collect(Collectors.toList());
}
简单的测试,但没有成功完成:
@Test
public void testExercise2() {
Lesson1 lesson1 = new Lesson1();
List<String> list = new ArrayList<>(Arrays.asList("alpha", "bravo", "charlie",
"delta", "echo", "foxtrot"));
List<String> inputList = new ArrayList<>(list);
lesson1.exercise2(list);
assertEquals("Input " + inputList.toString(), Arrays.asList("echo"), list);
}
答案 0 :(得分:3)
Streams永远不会修改原始列表。
您的函数会创建一个新列表,然后忽略它。