2013-04-30 84 views
0

我有一個Check Entity我有三個屬性,包括Id和我使用Id作爲散列碼以便使用和檢查重複項。如何從arraylist中刪除重複項(舊對象)

現在重複越來越使用下面的代碼

Set<Check> unique = new LinkedHashSet<Check>(l); 
List<Check> finalLst= new java.util.ArrayList<Check>(); 
finalLst.addAll(unique); 

在輸出

這三個都來爲結果移除(C1,C2和C3),但我想(C4,C5和C6)。

Check c1 = new Check(1,"one"); 
Check c2 = new Check(2,"two"); 
Check c3 = new Check(3,"three"); 


    Check c4 = new Check(1,"one"); 
    Check c5 = new Check(2,"two"); 
    Check c6 = new Check(3,"three"); 

輸出我得到現在:

id :1 ::2013-04-30 10:42:34.311 
    id :2 ::2013-04-30 10:42:34.344 
    id :3 ::2013-04-30 10:42:34.344 
    id :1 ::2013-04-30 10:42:34.344 
    id :2 ::2013-04-30 10:42:34.345 
    id :3 ::2013-04-30 10:42:34.345 
1 :: 2013-04-30 10:42:34.311 
2 :: 2013-04-30 10:42:34.344 
3 :: 2013-04-30 10:42:34.344 

輸出很期待:

id :1 ::2013-04-30 10:42:34.311 
    id :2 ::2013-04-30 10:42:34.344 
    id :3 ::2013-04-30 10:42:34.344 
    id :1 ::2013-04-30 10:42:34.344 
    id :2 ::2013-04-30 10:42:34.345 
    id :3 ::2013-04-30 10:42:34.345 
1 :: 2013-04-30 10:42:34.344 
2 :: 2013-04-30 10:42:34.345 
3 :: 2013-04-30 10:42:34.345 

我整個的代碼放在這裏:

package test.collection; 

import java.text.SimpleDateFormat; 
import java.util.*; 

public class RemoveDuplicateInArrayList 
{ 
    public static void main(String args[]) 
    { 
     Check c1 = new Check(1,"one"); 
     Check c2 = new Check(2,"two"); 
     Check c3 = new Check(3,"three"); 


     Check c4 = new Check(1,"one"); 
     Check c5 = new Check(2,"two"); 
     Check c6 = new Check(3,"three"); 

     List<Check> l = new java.util.ArrayList<Check>(); 
     l.add(c1); 
     l.add(c2); 
     l.add(c3); 
     l.add(c4); 
     l.add(c5); 
     l.add(c6); 

     List<Check> finalLst= removeDuplicates(l); 
     Iterator<Check> iter = finalLst.iterator(); 
     while(iter.hasNext()) 
     { 
      Check temp = iter.next(); 
      System.out.println(temp.getId()+" :: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(temp.getCreationTme())); 
     } 

    } 
    public static List<Check> removeDuplicates(List<Check> l) 
    { 
     Set<Check> unique = new LinkedHashSet<Check>(l); 
     List<Check> finalLst= new java.util.ArrayList<Check>(); 
     finalLst.addAll(unique); 
     return finalLst; 
    } 
} 

class Check 
{ 
    public Check(int id,String name) 
    { 
     this.id = id; 
     this.name = name; 
     this.creationTme = new Date(); 
     System.out.println("id :"+this.id+" ::"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(this.getCreationTme())); 
    } 
    private int id; 
    private String name; 
    private Date creationTme; 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 


    public Date getCreationTme() { 
     return creationTme; 
    } 
    public void setCreationTme(Date creationTme) { 
     this.creationTme = creationTme; 
    } 
    @Override 
    public int hashCode() 
    { 
     return this.id; 
    } 
    @Override 
    public boolean equals(Object obj) 
    { 
     if(obj instanceof Check && ((Check)obj).id == this.id) 
      return true; 
     else return false; 
    } 

} 
+2

下的選民,如果你有意識做出評論!!!!!!!!! – sunleo 2013-04-30 05:24:45

+0

謝謝你所有的答案............ – sunleo 2013-04-30 07:01:45

回答

0

使用地圖,而不是

import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 

public class RemoveDuplicateInArrayList 
{ 
    public static void main(String args[]) 
    { 
     Check c1 = new Check(1,"one"); 
     Check c2 = new Check(2,"two"); 
     Check c3 = new Check(3,"three"); 


     Check c4 = new Check(1,"one"); 
     Check c5 = new Check(2,"two"); 
     Check c6 = new Check(3,"three"); 

     Map<Integer, Check> map = new HashMap<Integer, Check>(); 
     map.put(c1.getId() , c1); 
     map.put(c2.getId() , c2); 
     map.put(c3.getId() , c3); 
     map.put(c4.getId() , c4); 
     map.put(c5.getId() , c5); 
     map.put(c6.getId() , c6); 

     System.out.println(map); 
    } 

} 

class Check 
{ 
    public Check(int id,String name) 
    { 
     this.id = id; 
     this.name = name; 
     this.creationTme = new Date(); 
     System.out.println("id :"+this.id+" ::"+this.getCreationTme().getTime()); 
    } 
    private Integer id; 
    private String name; 
    private Date creationTme; 
    public int getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 


    public Date getCreationTme() { 
     return creationTme; 
    } 
    public void setCreationTme(Date creationTme) { 
     this.creationTme = creationTme; 
    } 
    @Override 
    public int hashCode() 
    { 
     return this.id; 
    } 
    @Override 
    public boolean equals(Object obj) 
    { 
     if(obj instanceof Check && ((Check)obj).id == this.id) 
      return true; 
     else return false; 
    } 

    public String toString() 
    { 
     final String TAB = " "; 

     String retValue = ""; 

     retValue = "Check (" 
      + "id = " + this.id + TAB 
      + "name = " + this.name + TAB 
      + "creationTme = " + this.creationTme.getTime() + TAB 
      + ")"; 

     return retValue; 
    } 


} 

PS:我不是downvoter :)

0

您可以實現次ËComparable接口&覆蓋compareTo()方法在check類是這樣的: -

class Check implements Comparable<Check>{ 

compareTo()方法

@Override 
public int compareTo(Check o) { 
    if(creationTme.after(o.getCreationTme())){ 
     return -1; 
    }else if(creationTme.before(o.getCreationTme())){ 
     return 1; 
    } 
    return 0; 
} 

和去除重複之前,你可以Sort列表如下: -

Collections.sort(l); 
List<Check> finalLst = removeDuplicates(l); 
+0

我已經在這裏使用設置再次檢查我的問題>>>>>>>>>>>>> – sunleo 2013-04-30 05:24:06

+0

@sunleo - 我很倉促閱讀你的問題!現在查看我更新的答案! – SudoRahul 2013-04-30 05:56:41

0

Hashset的工作原理如下wa y: 如果您嘗試添加對象,則會計算哈希碼。如果哈希碼已經存在於地圖中,什麼都不會發生(爲什麼它應該是?哈希碼 - 因此等效的對象 - 已經在那裏) 所以你的輸出是完全合理的:)

如果你想有一個overwritehashset ,我會建議寫你自己的。

帶一個私人hashset的類。 添加方法首先檢查哈希碼是否已經存在,如果是:從集合中刪除值並添加新值。如果不是,只需添加它。

0

我已經修改了你的方法一點,這樣就可以得到預期的結果。基本上你想要最新的。所以在這裏,它是:

public static List<Check> removeDuplicates(List<Check> l) 
    { 
     Collections.reverse(l); 
     Set<Check> unique = new LinkedHashSet<Check>(l); 
     List<Check> finalLst= new java.util.ArrayList<Check>(); 
     finalLst.addAll(unique); 
     Collections.reverse(finalLst); 
     return finalLst; 
    } 

試試這個,它會工作