删除list中重复的对象

来源:互联网 发布:天猫交易额实时数据 编辑:程序博客网 时间:2024/05/17 01:28
  1. package test.com;   
  2.   
  3. import java.text.SimpleDateFormat;   
  4. import java.util.*;   
  5.   
  6. public class Test {   
  7.     public static void main(String[] args) {   
  8.         List<Account> list = new ArrayList<Account>();   
  9.         Calendar c = Calendar.getInstance();   
  10.         c.add(Calendar.SECOND, 1);   
  11.         Date date = c.getTime();   
  12.         Account account1 = new Account(1, Test.getFormat(date));   
  13.         c.add(Calendar.HOUR, 1);   
  14.         date = c.getTime();   
  15.         list.add(account1);   
  16.         list.add(account1);   
  17.         Account account2 = new Account(2, Test.getFormat(date));   
  18.         c.add(Calendar.HOUR, 1);   
  19.         date = c.getTime();   
  20.         list.add(account2);   
  21.         list.add(account2);   
  22.         Account account3 = new Account(3, Test.getFormat(date));   
  23.         c.add(Calendar.HOUR, 1);   
  24.         date = c.getTime();   
  25.         list.add(account3);   
  26.         list.add(account3);   
  27.         Account account4 = new Account(4, Test.getFormat(date));   
  28.         c.add(Calendar.HOUR, 1);   
  29.         date = c.getTime();   
  30.         list.add(account4);   
  31.         list.add(account4);   
  32.         Iterator it = list.iterator();   
  33.         if (list != null && list.size() != 0) {   
  34.             HashMap map = new HashMap();   
  35.             while (it.hasNext()) {   
  36.                 Account account = (Account) it.next();   
  37.                 Account accountmap = (Account) map   
  38.                         .get(account.getAccounttype());   
  39.                 if (accountmap == null) {   
  40.                     map.put(account.getAccounttype(), account);   
  41.                 } else {   
  42.                     it.remove();   
  43.                 }   
  44.             }   
  45.         }   
  46.         it = list.iterator();   
  47.         while (it.hasNext()) {   
  48.             System.out.println(it.next());   
  49.         }   
  50.     }   
  51.   
  52.     public static String getFormat(Date date) {   
  53.         SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");   
  54.         return dataFormat.format(date);   
  55.     }   
  56. }   
  57.   
  58. class Account {   
  59.     private int accounttype;   
  60.   
  61.     private String applyTime;   
  62.   
  63.     public Account(int accounttype, String applyTime) {   
  64.         this.accounttype = accounttype;   
  65.         this.applyTime = applyTime;   
  66.     }   
  67.   
  68.     public int getAccounttype() {   
  69.         return accounttype;   
  70.     }   
  71.   
  72.     public void setAccounttype(int accounttype) {   
  73.         this.accounttype = accounttype;   
  74.     }   
  75.   
  76.     public String getApplyTime() {   
  77.         return applyTime;   
  78.     }   
  79.   
  80.     public void setApplyTime(String applyTime) {   
  81.         this.applyTime = applyTime;   
  82.     }   
  83.   
  84.     public String toString() {   
  85.         return this.getApplyTime() + this.getAccounttype();   
  86.     }   
  87. }
原创粉丝点击