0

我有一个Check Entity我有三个属性的地方,包括 Id 并且正在使用 Id 作为哈希码,以便使用和检查重复项。

现在使用以下代码删除重复项

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;
    }

}
4

4 回答 4

0

Hashset 的工作方式如下:如果您尝试添加一个对象,则会计算它的哈希码。如果哈希码已经存在于地图中,则不会发生任何事情(为什么会发生?哈希码 - 因此等效的对象 - 已经存在)所以你的输出是完全合理的:)

如果您想拥有一个覆盖哈希集,我建议您自己编写。

参加带有私有哈希集的课程。add 方法首先检查哈希码是否已经存在,如果是:从集合中删除值并添加新值。如果没有:只需添加它。

于 2013-04-30T05:33:35.157 回答
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:我不是反对者:)

于 2013-04-30T05:34:13.873 回答
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);
于 2013-04-30T05:23:07.647 回答
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;
    }

试试这个,它会工作

于 2013-04-30T05:42:23.550 回答