在Java中,HashSet和LinkedHashSet可以自动去除重复元素,但它们实际上是集合(Set),而不是列表(List)。如果你想保持插入顺序并且去重,可以选择使用`java.util.LinkedHashSet`。 如果你确实需要一个具有去重功能的List,你可以先将元素添加到Set中进行去重,然后再转换为List: ```java List<String> listWithDuplicates = Arrays.asList("a", "b", "a", "c", "b"); Set<String> set = new LinkedHashSet<>(listWithDuplicates); List<String> listWithoutDuplicates = new ArrayList<>(set); ``` 这样,`listWithoutDuplicates`就是去重后的列表。 [2024-02-01 17:45:54 | AI写代码神器 | 138点数解答]