ArrayList去除重復元素(包括字符串和自定義對象)


1.去除重復字符串

package com.online.msym;
import java.util.ArrayList;
import java.util.Iterator;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_ArrayList {    
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("c");
        list.add("c");
        list.add("c");
        
        ArrayList newList = getSingle(list);
        System.out.println(newList);
    }
    /*
     * 創建新集合將重復元素去掉
     * 1,明確返回值類型,返回ArrayList
     * 2,明確參數列表ArrayList
     * 
     * 分析:
     * 1,創建新集合
     * 2,根據傳入的集合(老集合)獲取迭代器
     * 3,遍歷老集合
     * 4,通過新集合判斷是否包含老集合中的元素,如果包含就不添加,如果不包含就添加
     */
    public static ArrayList getSingle(ArrayList list) {
        ArrayList tempList = new ArrayList();                    //1,創建新集合
        Iterator it = list.iterator();                            //2,根據傳入的集合(老集合)獲取迭代器
        
        while(it.hasNext()) {                                    //3,遍歷老集合
            Object obj = it.next();                                //記錄住每一個元素
            if(!tempList.contains(obj)) {                        //如果新集合中不包含老集合中的元素
                tempList.add(obj);                                //將該元素添加
            }
        }    
        
        return tempList;
    }
}

2.去除ArrayList中重復自定義對象元素

注意事項:

           必須重寫equals()方法的,因為contains方法和remove方法底層都依賴於equals方法

package com.online.msym;
import java.util.ArrayList;
import java.util.Iterator;
import online.msym.bean.Person;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo2_ArrayList {
    /**
     * * 需求:ArrayList去除集合中自定義對象元素的重復值(對象的成員變量值相同,即同姓名同年齡)
         :注意事項:  重寫equals()方法的
        contains方法判斷是否包含,底層依賴的是equals方法 remove方法判斷是否刪除,底層依賴的是equals方法
     */
    public static void main(String[] args) {
        ArrayList list = new ArrayList();                //創建集合對象
         list.add(new Person("張三", 23));
        list.add(new Person("張三", 23));
        list.add(new Person("李四", 24));
        list.add(new Person("李四", 24));
        list.add(new Person("李四", 24));
        list.add(new Person("李四", 24));
        
        //ArrayList newList = getSingle(list);            //調用方法去除重復
        //System.out.println(newList);
        list.remove(new Person("張三", 23));
        System.out.println(list);
    }
    /*
     * 創建新集合將重復元素去掉
     * 1,明確返回值類型,返回ArrayList
     * 2,明確參數列表ArrayList
     * 
     * 分析:
     * 1,創建新集合
     * 2,根據傳入的集合(老集合)獲取迭代器
     * 3,遍歷老集合
     * 4,通過新集合判斷是否包含老集合中的元素,如果包含就不添加,如果不包含就添加
     */
    public static ArrayList getSingle(ArrayList list) {
        ArrayList tempList = new ArrayList<>();                    //1,創建新集合
        Iterator it = list.iterator();                            //2,根據傳入的集合(老集合)獲取迭代器
        
        while(it.hasNext()) {                                    //3,遍歷老集合
            Object obj = it.next();                                //記錄住每一個元素
            if(!tempList.contains(obj)) {                        //如果新集合中不包含老集合中的元素
                tempList.add(obj);                                //將該元素添加
            }
        }
        return tempList;
    }
}

 

Person實體類:

package online.msym.bean;
public class Person {
    private String name;
    private int age;
    public Person() {
        super();
        
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = 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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    //重寫equals方法,用於判斷連個Person對象是否相同
    @Override
    public boolean equals(Object obj) {
        Person p = (Person)obj;
        System.out.println("equals 方法被調用了,證明contains方法底層調用的是equals");
        return this.name.equals(p.name) && this.age == p.age;
    }
    
}

【點擊此處回到主頁】


免責聲明!

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



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