判斷ArryaList有沒有重復對象的方法


  ArrayList類是List類下一種常用的子類,如果要判斷容器里面的對象是否有相等,有兩種方法。

  下面是自定義的一個Student類,假設容器里重復是按照對象的兩個屬性都相等。

/**
 * @author Wangchengan
 *
 */
public class Student {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    public boolean equals(Object obj) {
        //增加效率,如果是相同對象就不用比較了
        if(this==obj)
        return true;
        //如果要比較的不是跟這個對象一樣類型的就拋出一個運行時異常
        if (!(obj instanceof Student)) {
            throw new ClassCastException("類型錯誤");
        }
        //向下轉型才能使用子類的方法
        Student student=(Student)obj;
        return this.name.equals(student.name)&&this.age==student.age;
    }
    
}

  下面是判斷的兩種方法,先使用了字符串作為演示。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListTest {

    public static void main(String[] args) {
        /*
         * ArrayList兩種判斷重復方法
        1.遍歷,使用兩個循環
        2.創建一個新的容器,舊容器中使用迭代器,沒有則加入新容器
        (其實contains方法里面也是使用了equls方法,只不過字符串的equals被復寫了)
        */
        List list=new ArrayList();
        list.add("abc1");
        list.add("abc4");
        list.add("abc2");
        list.add("abc1");
        list.add("abc4");
        list.add("abc4");
        list.add("abc2");
        list.add("abc1");
        list.add("abc4");
        list.add("abc2");
        
        System.out.println(list);
        singleElement2(list);
        System.out.println(list);
        System.out.println("------------");
        /*
         * ArrayList判斷對象的方法
         * 其實Arraylist里的contains方法是繼承自Collection接口里的contains方法,使用的是equals方法
         * 所以要判斷對象里的屬性的話,還是要復寫對象類里的euals方法,同時它會自動調用
         * 
         */
        List studentList=new ArrayList();
        Student stu1=new Student("lisi", 20);
        Student stu2=new Student("wangwu", 22);
        Student stu3=new Student("liqing", 23);
        Student stu4=new Student("liuhu", 27);
        Student stu5=new Student("lisi", 20);
        Student stu6=new Student("liqing", 23);
        Student stu7=new Student("lisi", 20);
        studentList.add(stu1);
        studentList.add(stu2);
        studentList.add(stu3);
        studentList.add(stu4);
        studentList.add(stu5);
        System.out.println(studentList);
        singleObject(studentList);
        System.out.println(studentList);
        
        

    }
    //判斷Arraylist里面有沒有重復對象的方法(給對象使用)
    private static void singleObject(List studentList) {
        singleElement2(studentList);
        
    }
    
    //判斷Arraylist里面有沒有重復對象的方法2
    private static void singleElement2(List list) {
        List newList=new ArrayList();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Object obj = (Object) iterator.next();
            
            if (!newList.contains(obj)) {
                newList.add(obj);
            }
        }
        list.clear();
        list.addAll(newList);
        
    }
    //判斷Arraylist里面有沒有重復對象的方法1
    private static void singleElement(List list) {
        for (int i = 0; i < list.size()-1; i++) {
            Object obj_x=list.get(i);
            for (int j = i+1; j< list.size(); j++) {
                if (obj_x.equals(list.get(j))) {
                    list.remove(j);
                    //每次都要減去一個,因為remove后數據元素會改變
                    j--;
                }
                
            }
        }
        
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM