List集合去重的一種方法 z

2021-09-08 17:49:44 字數 385 閱讀 4279

需要對乙個list集合去重,情況是該集合中會出現多個name屬性值相同的,但是其他屬性值不同的資料。

在這種情況下,需求要只保留其中乙個就好。

我覺得遍歷和hashset都不是我想要的,便採用了一下方式

定義compare類,繼承iequalitycomparer介面

public class comparerhelper : iequalitycomparer

public int gethashcode(model obj)

}

呼叫如下:

modellist = modellist.distinct(new comparerhelper()).tolist();

List集合去重的一種方法

前一段時間們需要對乙個list集合去重,情況是該集合中會出現多個name屬性值相同的,但是其他屬性值不同的資料。在這種情況下,需求要只保留其中乙個就好。我覺得遍歷和hashset都不是我想要的,便採用了一下方式 定義compare類,繼承iequalitycomparer介面 public clas...

針對list集合去重的幾種方法

這種方法就相對比較死板,必須集合的物件中每個欄位都相同才能去重。public static void main string args public static void remove system.out.println 迴圈去重後的集合 users.tostring 執行結果 迴圈去重前的集合...

4種 List去重的方法

使用兩個for迴圈實現list去重 public static list repeatlistwayone listlist return list 使用hashset實現list去重 public static list repeatlistwaytwo listlist 使用treeset實現l...