首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Java List 去重一行代碼實現:HashSet 構造器、Stream API 與並發場景選型

     編輯:Java 綜合教學

Java List 去重一行代碼實現:HashSet 構造器、Stream API 與並發場景選型

List 去重是高頻操作,本文總結三種主流寫法、性能特點與適用場景。

寫法對比

JDK 版本代碼特點
所有版本List<T> uniq = new ArrayList<>(new HashSet<>(list));最簡潔,利用 HashSet 去重特性,不保留原順序。
Java 7+List<T> uniq = new ArrayList<>(new HashSet<>(list)); (鑽石語法)同上,類型推斷更簡潔。
Java 8+List<T> uniq = list.stream().distinct().collect(Collectors.toList());保留原順序,函數式風格,可並行 .parallelStream()。
Java 8+List<T> uniq = new ArrayList<>(new LinkedHashSet<>(list));保留原順序,性能優於 Stream,推薦生產環境。

完整示例

import java.util.*;

public class ListDeduplicateDemo {
    public static void main(String[] args) {
        List listWithDup = Arrays.asList("1", "2", "3", "1", "2", "4");

        // 1. HashSet - 無序
        List unordered = new ArrayList<>(new HashSet<>(listWithDup));
        System.out.println("HashSet: " + unordered); // [1, 2, 3, 4] 順序不定

        // 2. LinkedHashSet - 有序
        List ordered = new ArrayList<>(new LinkedHashSet<>(listWithDup));
        System.out.println("LinkedHashSet: " + ordered); // [1, 2, 3, 4] 保留首次出現順序

        // 3. Stream distinct - 有序
        List streamed = listWithDup.stream().distinct().collect(Collectors.toList());
        System.out.println("Stream: " + streamed); // [1, 2, 3, 4]
    }
}

性能與選型建議

  • 小數據量(<1萬):任意方式性能差異可忽略,推薦 LinkedHashSet 兼顧有序與簡潔。
  • 大數據量/高頻調用:LinkedHashSet 單次遍歷 O(N),常數因子最小;Stream 多一次 Lambda 開銷。
  • 並發環境:使用 CopyOnWriteArrayList + LinkedHashSet,或並發流 list.parallelStream().distinct()(注意順序不保證)。
  • 自定義對象:必須正確重寫 equals() 與 hashCode(),否則去重失效。

常見坑

  1. 直接 new HashSet(list) 後再 list.clear(); list.addAll(set) 會改變原列表引用,影響其他持有者。
  2. 基本類型列表(List<int>)不可用,需裝箱 List<Integer>。
  3. null 元素:HashSet 允許單個 null,distinct() 同樣保留一個 null。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved