Java API —— Collections類


1、Collections類概述
        針對集合操作 的工具類,都是靜態方法
 
2、Collections成員方法
        public static <T> void sort(List<T> list):排序 默認情況下是自然順序。
        public static <T> int binarySearch(List<?> list,T key):二分查找
        public static <T> T max(Collection<?> coll):最大值
        public static void reverse(List<?> list):反轉
        public static void shuffle(List<?> list):隨機置換
 
例子1:存儲基本包裝類
package collectiondemos;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
 * Created by gao on 15-12-22.
 */
public class CollectionsDemo01 {
    public static void main(String[] args) {
        // 創建集合對象
        List<Integer> list = new ArrayList<Integer>();
        // 添加元素
        list.add(30);
        list.add(20);
        list.add(50);
        list.add(10);
        list.add(40);
        System.out.println("list:" + list); //list:[30, 20, 50, 10, 40]
        // public static <T> void sort(List<T> list):排序 默認情況下是自然順序。
        //Collections.sort(list);
        //System.out.println("list:" + list); //list:[10, 20, 30, 40, 50]
        // public static <T> int binarySearch(List<?> list,T key):二分查找
        System.out.println("binarySearch:" + Collections.binarySearch(list, 30)); //binarySearch:2
        System.out.println("binarySearch:" + Collections.binarySearch(list, 300)); //binarySearch:-6
        // public static <T> T max(Collection<?> coll):最大值
        System.out.println("max:" + Collections.max(list)); //max:50
        // public static void reverse(List<?> list):反轉
//        Collections.reverse(list);
//        System.out.println("list:"+list); //list:[40, 10, 50, 20, 30]
        //public static void shuffle(List<?> list):隨機置換
        Collections.shuffle(list);
        System.out.println("list:" + list); //list:[20, 50, 10, 30, 40]
    }
}

 

例子2:存儲自定義對象
學生類:
package collectiondemos;
public class Student implements Comparable<Student>{
    private String name;
    private int age;
    public Student() {
        super();
    }
    public Student(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 int compareTo(Student s) {
        int num = this.age - s.getAge();
        int num2 = num == 0 ? this.name.compareTo(s.getName()) : num;
        return num2;
    }
}

測試類:

package collectiondemos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * Created by gao on 15-12-22.
 */
public class CollectionsDemo02 {
    public static void main(String[] args) {
        // 創建集合對象
        List<Student> list = new ArrayList<Student>();
        // 創建學生對象
        Student s1 = new Student("林青霞", 27);
        Student s2 = new Student("風清揚", 30);
        Student s3 = new Student("劉曉曲", 28);
        Student s4 = new Student("武鑫", 29);
        Student s5 = new Student("林青霞", 27);
        // 添加元素對象
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        // 排序
        // 方式一:自然排序
        // Collections.sort(list);
        // 方式二:比較器排序
        // 如果同時有自然排序和比較器排序,以比較器排序為主
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s2.getAge() - s1.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });
        // 遍歷集合
        for (Student s : list) {
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
輸出結果:
風清揚---30
武鑫---29
劉曉曲---28
林青霞---27
林青霞---27

 

 


免責聲明!

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



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