Day10_1 JavaSE Collections工具類

JavaSE 操作集合的工具類 : Collections

Collections是一個操作Set、List和Map等集合的工具類。

Collections中提供了大量方法對集合元素進行排序、查詢和修改等操作,還提供了對集合對象設置不可變、對集合對象實現同步控制等方法。

1 方法整理

  • 排序方法:

    函數名稱 函數功能
    reverse(List) 反轉List中元素順序
    shuffle(List) 對List集合元素隨機排序
    sort(List) 根據字典順序對List集合按升序排序
    sort(List,Comparator) 根據指定的Comparator產生的順序對List集合元素進行排序
    swap(List, int , int ) 將List集合中i處元素和j處元素進行交換
  • 查找、替換方法:

    函數名稱 函數功能
    Collections.max(List) 根據字典順序返回集合中的最大元素
    Collections.min(List) 根據字典順序返回集合中的最小元素
    Collections.max(List, Comparator) 根據定製順序返回最大元素
    Collections.min(List, Comparator) 根據定製順序返回最小元素
    Collections.frequency(List, Object) 返回集合中指定元素出現次數
    boolean replaceAll(List, oldVal, newVal) 使用新值替換List對象的所有舊值

2 案例展示

  • 排序方法展示

    package com.collections;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Demo01 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("1");
            System.out.println(list);
            System.out.println("--------------------------");
    
            Collections.reverse(list); //反轉list元素順序
            System.out.println(list);
            System.out.println("--------------------------");
    
    
            Collections.shuffle(list); //對list元素進行隨機排序
            System.out.println(list);
            System.out.println("--------------------------");
    
    
            Collections.sort(list); ///按照字典升序排列
            System.out.println(list);
            System.out.println("--------------------------");
    
            Collections.swap(list,0,4); //指定位置的元素交換
            System.out.println(list);
            System.out.println("--------------------------");
        }
    }
    /*運行結果:
    [a, b, c, d, 1]
    --------------------------
    [1, d, c, b, a]
    --------------------------
    [a, c, b, 1, d]
    --------------------------
    [1, a, b, c, d]
    --------------------------
    [d, a, b, c, 1]
    --------------------------
    */
    
  • 按照定製順序排序

    package com.collections;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Demo01 {
        public static void main(String[] args) {
            Student s1 = new Student(18,"Jever");
            Student s2 = new Student(17,"Calie");
            Student s3 = new Student(20,"Tony");
            Student s4 = new Student(19,"Demut");
    
            List<Student> stus = new ArrayList<Student>();
            stus.add(s1);
            stus.add(s2);
            stus.add(s3);
            stus.add(s4);
            System.out.println("原順序:");
            for (Student stu : stus){
                System.out.println(stu.name+","+stu.age);
            }
    
            Collections.sort(stus, new Student());
            System.out.println("排序後結果:");
            for (Student stu : stus){
                System.out.println(stu.name+","+stu.age);
            }
        }
    
    }
    class Student implements Comparator<Student>{
        int age;
        String name;
    
        public Student(){
    
        }
        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }
    
        @Override
        public int compare(Student o1, Student o2) { //按照升序排列
            if (o1.age > o2.age){
                return 1;
            }else if(o1.age < o2.age){
                return -1;
            }else{
                return 0;
            }
        }
    }
    /*運行結果:
    原順序:
    Jever,18
    Calie,17
    Tony,20
    Demut,19
    排序後結果:
    Tony,20
    Demut,19
    Jever,18
    Calie,17
    */
    
  • 查找、替換方法

    package com.collections;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Demo01 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("1");
            System.out.println(list);
    
            System.out.println(Collections.max(list)); //返回集合最大元素
            System.out.println(Collections.min(list)); //返回集合最小元素
    
            System.out.println(Collections.frequency(list,"a"));//返回集合指定元素的出現次數
    
            Collections.replaceAll(list,"a","aa");//使用新值替換舊值
    
            System.out.println(list);
    
            Student s1 = new Student(18,"Jever");
            Student s2 = new Student(17,"Calie");
            Student s3 = new Student(20,"Tony");
            Student s4 = new Student(19,"Demut");
    
            List<Student> stus = new ArrayList<Student>();
            stus.add(s1);
            stus.add(s2);
            stus.add(s3);
            stus.add(s4);
    
            Student stu1 = Collections.max(stus, new Student());
            System.out.println(stu1.name+","+stu1.age);
    
            Student stu2 = Collections.min(stus, new Student());
            System.out.println(stu2.name+","+stu2.age);
        }
    
    }
    
    class Student implements Comparator<Student>{
        int age;
        String name;
    
        public Student(){
    
        }
        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }
    
        @Override
        public int compare(Student o1, Student o2) { //按照升序排列
            if (o1.age > o2.age){
                return 1;
            }else if(o1.age < o2.age){
                return -1;
            }else{
                return 0;
            }
        }
    }
    /*運行結果:
    [a, b, c, d, 1]
    d
    1
    1
    [aa, b, c, d, 1]
    Tony,20
    Calie,17
    */
    

寫在最後

沒有天生的信心,只有不斷培養的信心。

To Demut and Dottie!

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