我有Check Entity
我有三个属性,包括Id和我使用Id for hashcode以便使用和检查重复项。
现在使用以下代码删除重复项
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;
}
}
答案 0 :(得分:0)
您可以实施Comparable
界面&amp;覆盖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);
答案 1 :(得分:0)
Hashset的工作方式如下: 如果您尝试添加对象,则会计算其哈希码。如果哈希码已经存在于地图中,那么什么都不会发生(为什么要这样?哈希码 - 因此等效的对象 - 已经在那里) 所以你的输出是完全合理的:)
如果你想要一个overwritehashset,我建议你自己编写。
带一个私有hashset的类。 添加方法首先检查哈希码是否已经存在,如果是:从集合中删除值并添加新值。如果不是:只需添加即可。
答案 2 :(得分: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:)
答案 3 :(得分: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;
}
试试这个,它会起作用