找出list中的不同元素、删除两个list中相同的对象

  1. package com.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.Collections;  
  6. import java.util.List;  
  7.   
  8. /** 
  9.  *  
  10.  * 找出两个list中不同的元素 
  11.  * @author leiwei 2011-12-14 
  12.  * 
  13.  */  
  14. public class NoEqualsElement {  
  15.   
  16.     public static void main(String[] args) {  
  17.         List<String> list1 = new ArrayList<String>();  
  18.         List<String> list2 = new ArrayList<String>();  
  19.           
  20.   
  21.         list1.add("1");  
  22.         list1.add("3");  
  23.         list1.add("8");  
  24.         list1.add("9");  
  25.   
  26.         list2.add("2");  
  27.         list2.add("3");  
  28.         list2.add("7");  
  29.   
  30.         for(String s2:list1) {  
  31.             boolean flag = false;  
  32.             for(String s1:list2) {  
  33.                 if(s2.equals(s1)) {  
  34.                     flag = true;  
  35.                     break;  
  36.                 }  
  37.             }  
  38.             if(!flag){  
  39.                 System.out.println(s2);  
  40.             }  
  41.         }  
  42.     }  
  43. }  
  1. package com.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashSet;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Set;  
  8.   
  9. import com.model.Student;  
  10.   
  11. /** 
  12.  * 重写对象Student的equals方法 
  13.  * 只要age相等 对象就相等 
  14.  *  
  15.  * 删除两个集合中 相同的对象只保留一个 
  16.  * @author leiwei 2011-12-19 
  17.  * 
  18.  */  
  19. public class ListOBJ {  
  20.   
  21.     public static void main(String[] args) {  
  22.         Set<Student> setStudent = new HashSet<Student>();  
  23.           
  24.         List<Student> list1 = new ArrayList<Student>();  
  25.         List<Student> list2 = new ArrayList<Student>();  
  26.           
  27.         Student s1 = new Student();  
  28.         s1.setAge("10");  
  29.         list1.add(s1);  
  30.         Student s2 = new Student();  
  31.         s2.setAge("20");  
  32.         list1.add(s2);  
  33.           
  34.         Student s3 = new Student();  
  35.         s3.setAge("20");  
  36.         list2.add(s3);  
  37.         Student s4 = new Student();  
  38.         s4.setAge("30");  
  39.         list2.add(s4);  
  40.           
  41.         /** 
  42.          * 我们知道set集合,中的元素不会重复 
  43.          *  
  44.          * 将连个对象集合放进去,自然就会排出重复的 
  45.          *  
  46.          * 对象(前提Student 对象重写了equals方法) 
  47.          */  
  48.         setStudent.addAll(list1);  
  49.         setStudent.addAll(list2);  
  50.           
  51.         //输出 测试看是否 排出成功  
  52.         System.out.println(setStudent.size());  
  53.           
  54.         Iterator<Student> iterator = setStudent.iterator();  
  55.         while (iterator.hasNext()) {  
  56.             Student s = iterator.next();  
  57.             System.out.println(s.getAge());  
  58.         }  
  59.           
  60.     }  
  61. }  
  1. package com.model;  
  2.   
  3. public class Student {  
  4.   
  5.     private String age;  
  6.   
  7.     public String getAge() {  
  8.         return age;  
  9.     }  
  10.   
  11.     public void setAge(String age) {  
  12.         this.age = age;  
  13.     }  
  14.   
  15.     //重写equals方法只要age相等,我们就认为对象两个相等  
  16.     @Override  
  17.     public boolean equals(Object obj) {  
  18.         if(obj instanceof Student){  
  19.             Student st=(Student) obj;  
  20.             return (age.equals(st.age));  
  21.         }else{  
  22.             return super.equals(obj);  
  23.         }  
  24.     }  
  25.   
  26.     @Override  
  27.     public int hashCode() {  
  28.   
  29.         return age.hashCode();  
  30.     }  
  31.   
  32.   
  33. }  

转自:http://blog.csdn.net/goodleiwei/article/details/7083199

原文地址:https://www.cnblogs.com/flywang/p/5291339.html