删除list中重复的对象


package test.com;

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

public class Test {
public static void main(String[] args) {
List<Account> list = new ArrayList<Account>();
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, 1);
Date date = c.getTime();
Account account1 = new Account(1, Test.getFormat(date));
c.add(Calendar.HOUR, 1);
date = c.getTime();
list.add(account1);
list.add(account1);
Account account2 = new Account(2, Test.getFormat(date));
c.add(Calendar.HOUR, 1);
date = c.getTime();
list.add(account2);
list.add(account2);
Account account3 = new Account(3, Test.getFormat(date));
c.add(Calendar.HOUR, 1);
date = c.getTime();
list.add(account3);
list.add(account3);
Account account4 = new Account(4, Test.getFormat(date));
c.add(Calendar.HOUR, 1);
date = c.getTime();
list.add(account4);
list.add(account4);
Iterator it = list.iterator();
if (list != null && list.size() != 0) {
HashMap map = new HashMap();
while (it.hasNext()) {
Account account = (Account) it.next();
Account accountmap = (Account) map
.get(account.getAccounttype());
if (accountmap == null) {
map.put(account.getAccounttype(), account);
} else {
it.remove();
}
}
}
it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}

public static String getFormat(Date date) {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return dataFormat.format(date);
}
}

class Account {
private int accounttype;

private String applyTime;

public Account(int accounttype, String applyTime) {
this.accounttype = accounttype;
this.applyTime = applyTime;
}

public int getAccounttype() {
return accounttype;
}

public void setAccounttype(int accounttype) {
this.accounttype = accounttype;
}

public String getApplyTime() {
return applyTime;
}

public void setApplyTime(String applyTime) {
this.applyTime = applyTime;
}

public String toString() {
return this.getApplyTime() + this.getAccounttype();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值