java的list去重
时间: 2023-09-01 19:10:05 浏览: 96
在Java中,有多种方法可以对List进行去重操作。
一种常见的方法是使用双重for循环来遍历List并比较元素,然后移除重复的元素。这种方法可以通过使用ArrayList的remove方法来实现。以下是一个示例代码:
```java
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (i != j && list.get(i) == list.get(j)) {
list.remove(list.get(j));
}
}
}
System.out.println(list);
```
这段代码会输出去重后的list:。。具体使用哪种方法可以根据实际需求和性能要求来选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [List去除重复数据的几种方法](https://blog.csdn.net/A_Java_Dog/article/details/109765550)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Java【List】去重的 6种方法](https://blog.csdn.net/weixin_43825761/article/details/127778880)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文