Java中ArrayList的去重複及排序

去重複

思想:首先創建一個新都集合,通過遍歷舊的集合,用舊集合的元素判斷新集合當中是否有該元素,沒有就添加到新集合當中。

字符類型元素去重複

package com.lj.test;

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {

        ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("hello");
        arrayList.add("hello");
        arrayList.add("hello");
        arrayList.add("world");
        arrayList.add("world");
        arrayList.add("java");

        ArrayList<String> arrayListnew = new ArrayList<>();
        for(String s:arrayList){
            if(!arrayListnew.contains(s)){
                arrayListnew.add(s);
                //便利舊集合沒有就添加到新集合
            }
        }
        System.out.println("arraylistnew:" + arrayListnew);
    }
}

運行結果如下:
arraylistnew:[hello, world, java]

自定義對象去重複

因爲要比較自定義對象首先我們肯定要創建一個自定義對象


package com.lj.test;

public class Student {

    private String studnet;
    private int age;

    public Student() {
        super();
    }

    public Student(String studnet, int age) {
        super();
        this.studnet = studnet;
        this.age = age;
    }

    public String getStudnet() {
        return studnet;
    }

    public void setStudnet(String studnet) {
        this.studnet = studnet;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [studnet=" + studnet + ", age=" + age + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (studnet == null) {
            if (other.studnet != null)
                return false;
        } else if (!studnet.equals(other.studnet))
            return false;
        return true;
    }


}

簡單定義一個學生類,傳入名字及年齡即可。需要特別注意的是由於我們要比較學生對象裏面的名字及年齡是否相等才能判斷該對象是否相等所以這裏需要重寫學生類的equals()方法,有ecplise自動生成即可

回到main方法中

package com.lj.test;

import java.util.ArrayList;

public class ArrayListDemo2 {
    public static void main(String[] args) {

        ArrayList<Student> arrayList = new ArrayList<>();
        ArrayList<Student> arrayListnew = new ArrayList<>();

        Student s1 = new Student("pirlo",21);
        Student s2 = new Student("pirlo",21);
        Student s3 = new Student("範廚師",22);
        Student s4 = new Student("馬師傅",21);
        Student s5 = new Student("非洲德化",24);
        Student s6 = new Student("pirlo",22);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        arrayList.add(s6);

        for(Student s :arrayList){
            System.out.println(s);
            if(!arrayListnew.contains(s)){
                arrayListnew.add(s);
            }
        }
        System.out.println("---------------------");

        for(Student s:arrayListnew){
            System.out.println(s);
        }
    }
}

首先新建兩個集合,然後創建學生類對象,將學生類對象添加到ArrayList中,遍歷ArrayList如果沒有就將其添加進ArrayListnew中。
對兩個集合進行打印得到結果如下:
Student [studnet=pirlo, age=21]
Student [studnet=pirlo, age=21]
Student [studnet=範廚師, age=22]
Student [studnet=馬師傅, age=21]
Student [studnet=非洲德化, age=24]
Student [studnet=pirlo, age=22]
ArrayList如上
———————
ArrayListnew如下
Student [studnet=pirlo, age=21]
Student [studnet=範廚師, age=22]
Student [studnet=馬師傅, age=21]
Student [studnet=非洲德化, age=24]
Student [studnet=pirlo, age=22]

集合排序

思路:使用Collections.sort()方法進行排序

字符集合排序

package com.lj.test;

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListDemo3 {
    public static void main(String[] args) {

        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("adobe");
        arrayList.add("piack");
        arrayList.add("錘子");
        arrayList.add("手");
        arrayList.add("下面是中文");
        arrayList.add("我是中文");
        arrayList.add("上面是中文加1");
        arrayList.add("敖廠長");
        arrayList.add("王尼瑪");
        arrayList.add("back");
        arrayList.add("aeobe");

        Collections.sort(arrayList);
        System.out.println(arrayList);
    }
}

運行結果如下:
[adobe, aeobe, back, piack, 上面是中文加1, 下面是中文, 我是中文, 手, 敖廠長, 王尼瑪, 錘子]
觀察結果我們可以看到,對英文字符串是按字母自然排序規則進行,中文沒有看出來待覈實

自定義對象排序

同樣我們首先自定義一個學生類


package com.lj.test;

public class Student implements Comparable<Student>{

    private String name;
    private int age;

    public Student() {
        super();
    }

    public Student(String studnet, int age) {
        super();
        this.name = studnet;
        this.age = age;
    }

    public String getStudnet() {
        return name;
    }

    public void setStudnet(String studnet) {
        this.name = studnet;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [studnet=" + name + ", age=" + age + "]";
    }

    @Override
    public int compareTo(Student o) {
        int num = this.age - o.age;
        int num2 = num == 0 ?this.name.compareTo(o.name):num;
        return num2;
    }   
}

需要注意的是要對自定義類實現排序我們必須在這個類實現一個Comparable接口並複寫其compareTo()方法。

再次回到MainActivity中:

package com.lj.test;

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListDemo3 {
    public static void main(String[] args) {

        ArrayList<Student> arrayList = new ArrayList<Student>();

        Student s1 = new Student("pirlo",21);
        Student s3 = new Student("範廚師",23);
        Student s4 = new Student("馬師傅",21);
        Student s5 = new Student("非洲德化",24);
        Student s6 = new Student("pirlo",22);

        arrayList.add(s1);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        arrayList.add(s6);

        Collections.sort(arrayList);
        for(Student s : arrayList){
            System.out.println(s);
        }
    }
}

運行結果:
Studnet [name=pirlo, age=21]
Studnet [name=馬師傅, age=21]
Studnet [name=pirlo, age=22]
Studnet [name=範廚師, age=23]
Studnet [name=非洲德化, age=24]

已經可以看到我們對學生們按年齡進行了排序。

有了以上兩種方法之後我們就可以使用Arraylist進行去重複和排序功能了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章