Java List實體類去重

今天寫了一段代碼判斷list中是否包含某個實體openSubjectDTO,不包含就添加進去,發現很差使java

if(!subjectList.contains(openSubjectDTO)){
subjectList.add(openSubjectDTO);
}ide

後來發現是openSubjectDTO類中缺乏兩個方法以下:this

List 去除重複Object對象:spa

    對象重複是指對象裏面的變量的值都相等,並不定是地址。list集合存儲的類型是基礎類型還比較好辦,直接把list集合轉換成set集合就會自動去除。.net

   當set集合存儲的是對象類型時,須要在對象的實體類裏面重寫public boolean equals(Object obj) {} 和 public int hashCode() {} 兩個方法。對象

 

List特色:元素有放入順序,元素可重複 blog

Map特色:元素按鍵值對存儲,無放入順序 rem

Set特色:元素無放入順序,元素不可重複(注意:元素雖然無放入順序,可是元素在set中的位置是有該元素的HashCode決定的,其位置實際上是固定的) get

新建一個實體類hash

 

[java]  view plain copy
 
  1. public class User {  
  2.     private String id;  
  3.     private String name;  
  4.     private String age;  
  5.     private String address;  
  6.   
  7.     public String getId() {  
  8.         return id;  
  9.     }  
  10.   
  11.     public void setId(String id) {  
  12.         this.id = id;  
  13.     }  
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.   
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.   
  23.     public String getAge() {  
  24.         return age;  
  25.     }  
  26.   
  27.     public void setAge(String age) {  
  28.         this.age = age;  
  29.     }  
  30.   
  31.     public String getAddress() {  
  32.         return address;  
  33.     }  
  34.   
  35.     public void setAddress(String address) {  
  36.         this.address = address;  
  37.     }  
  38.   
  39.     @Override  
  40.     public int hashCode() {  
  41.         final int prime = 31;  
  42.         int result = 1;  
  43.         result = prime * result + ((id == null) ? 0 : id.hashCode());  
  44.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  45.         result = prime * result + ((age == null) ? 0 : age.hashCode());  
  46.         result = prime * result + ((address == null) ? 0 : address.hashCode());  
  47.         return result;  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean equals(Object obj) {  
  52.         if (this == obj) {  
  53.             return true;  
  54.         }  
  55.         if (obj == null) {  
  56.             return false;  
  57.         }  
  58.         if (getClass() != obj.getClass()) {  
  59.             return false;  
  60.         }  
  61.         User other = (User) obj;  
  62.         if (id == null) {  
  63.             if (other.id != null) {  
  64.                 return false;  
  65.             }  
  66.         } else if (!id.equals(other.id)) {  
  67.             return false;  
  68.         }  
  69.         if (name == null) {  
  70.             if (other.name != null) {  
  71.                 return false;  
  72.             }  
  73.         } else if (!name.equals(other.name)) {  
  74.             return false;  
  75.         }  
  76.         if (age == null) {  
  77.             if (other.age != null) {  
  78.                 return false;  
  79.             }  
  80.         } else if (!age.equals(other.age)) {  
  81.             return false;  
  82.         }  
  83.         if (address == null) {  
  84.             if (other.address != null) {  
  85.                 return false;  
  86.             }  
  87.         } else if (!address.equals(other.address)) {  
  88.             return false;  
  89.         }  
  90.         return true;  
  91.     }  
  92. }  

調用便可

 

 

[java]  view plain copy
 
    1. private static List<User> removeListDuplicateObject(List<User> list) {   
    2.     System.out.println(Arrays.toString(list.toArray()));   
    3.     Set<User> set = new HashSet<User>();   
    4.     set.addAll(list);   
    5.     System.out.println(Arrays.toString(set.toArray()));  
    6.     List<User> listnewList = new ArrayList<User>(set);  
    7.     return listnewList;  
    8.     }   
相關文章
相關標籤/搜索