我希望从ArrayList中删除重复项。
如果我这样做,它可以工作:
List<String> test = new ArrayList<>();
test.add("a");
test.add("a"); //Removing
test.add("b");
test.add("c");
test.add("c"); //Removing
test.add("d");
test = test.stream().distinct().collect(Collectors.toList());
但是,如果我想移除重复的String[]而不是String,它并没有移除重复项:
List<String[]> test = new ArrayList<>();
test.add(new String[]{"a", "a"});
test.add(new String[]{"a", "a"}); // Not removing
test.add(new String[]{"b", "a"});
test.add(new String[]{"b", "a"}); // Not removing
test.add(new String[]{"c", "a"});
test.add(new String[]{"c", "a"}); // Not removing
test = test.stream().distinct().collect(Collectors.toList());
ArrayList<String[]> test2 = (ArrayList<String[]>) test;
有没有解决这个问题的方法或者另一种去除
ArrayList<String[]>
中重复元素的方式?谢谢。
List<List<String>>
代替List<String[]>
,因为数组不会覆盖Object的equals方法。 - Eran