List去重

2010-05-26  林子新 

  1. package com.hoocy;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Iterator;   
  5. import java.util.List;   
  6.   
  7. public class TestUserC {   
  8.   
  9.     public static void main(String[] args) {   
  10.            
  11.         List list = new ArrayList<User>();   
  12.         List<User> ListA = new ArrayList<User>();   
  13.         List<User> ListB = new ArrayList<User>();   
  14.            
  15.         init(ListA, ListB);   
  16.            
  17.         if (ListA != null) {   
  18.             Iterator it = ListA.iterator();   
  19.             while (it.hasNext()) {   
  20.                 list.add((User) it.next());   
  21.             }   
  22.         }   
  23.         if (ListB != null) {   
  24.             Iterator it = ListB.iterator();   
  25.             while (it.hasNext()) {   
  26.                 User o = (User) it.next();   
  27.                 if (!list.contains(o))   
  28.                     list.add(o);   
  29.             }   
  30.         }   
  31.         for (int i = 0; i < list.size(); i++) {   
  32.             System.out.println(((User)list.get(i)).getName());   
  33.         }   
  34.            
  35.     }   
  36.     public static void init(List list,List list2){   
  37.         User user1 = new User("hoocy1","d","");   
  38.         User user2 = new User("hoocy2","d","");   
  39.         User user3 = new User("hoocy3","d","");   
  40.         User user4 = new User("hoocy4","d","");   
  41.         User user5 = new User("hoocy5","d","");   
  42.         list.add(user1);   
  43.         list.add(user2);   
  44.         list.add(user3);   
  45.         list.add(user4);   
  46.         list.add(user5);   
  47.            
  48.         User user6 = new User("hoocy1","d","");   
  49.         User user7 = new User("hoocy2","d","");   
  50.         User user8 = new User("hoocy3","d","");   
  51.         User user9 = new User("hoocy88","d","");   
  52.         User user10 = new User("hoocy55","d","");   
  53.            
  54.         list2.add(user10);   
  55.         list2.add(user6);   
  56.         list2.add(user7);   
  57.         list2.add(user8);   
  58.         list2.add(user9);   
  59.            
  60.     }   
  61.         
  62.   
  63.   
  64.   
  65.   
  66. }  
  67. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    1. package com.hoocy;   
    2.   
    3. public class User {   
    4.        
    5.     private String name;   
    6.     private String type;   
    7.     private String date;   
    8.     User(){}   
    9.        
    10.     User(String name,String type,String date)   
    11.     {   
    12.         this.name=name;   
    13.         this.type=type;   
    14.         this.date=date;   
    15.     }   
    16.        
    17.     public String getDate() {   
    18.         return date;   
    19.     }   
    20.     public void setDate(String date) {   
    21.         this.date = date;   
    22.     }   
    23.     public String getName() {   
    24.            
    25.         return name==null?"null":name;   
    26.     }   
    27.     public void setName(String name) {   
    28.         this.name = name;   
    29.     }   
    30.     public String getType() {   
    31.         return type;   
    32.     }   
    33.     public void setType(String type) {   
    34.         this.type = type;   
    35.     }   
    36.        
    37.     public boolean equals(Object obj) {   
    38.         if (this == obj)   
    39.             return true;   
    40.         if (obj == null)   
    41.             return false;   
    42.         if (getClass() != obj.getClass())   
    43.             return false;   
    44.         final User other = (User) obj;   
    45.         if(this.getName()!=other.getName())   
    46.         return false;   
    47.            
    48.         return true;   
    49.   
    50.    }   
    51.        
    52.        
    53.        
    54. }  
    55. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    56. 结果

      hoocy1
      hoocy2
      hoocy3
      hoocy4
      hoocy5
      hoocy55
      hoocy88

      google_protectAndRun("render_ads.js::google_render_ad", google_handleError, google_render_ad);
452°/4512 人阅读/1 条评论 发表评论

袁帅  2010-06-10

转了 ~


登录 后发表评论