1. 程式人生 > >javaAPI_集合基礎_Collections工具類

javaAPI_集合基礎_Collections工具類


Collections工具類

1.區分集合Collection和Collections,倆個是不同的一個單列集合的頂層介面,而另一個用於操作集合的工具類。

2.概述
Collections工具類是一個針對集合操作的工具類。並且都是靜態方法。

3.先關方法(僅列舉常用方法)
public static <T> void sort(List<T> lt):對一個集合進行自然排序,一般是升序排序。
public static <T> int binarySearch(List<T> lt,T key):二分查詢
public static <T> T Max(Collection<?> coll):最大值
public static void reverse(List<T> lt):反轉
public static void shuffle(List<T> lt):隨機置換

4.程式碼測試
public static void main(String[] args) {
//建立物件
List<Integer> lt = new ArrayList<Integer>();
//新增元素
lt.add(12);
lt.add(10);
lt.add(17);
lt.add(14);
lt.add(13);
lt.add(16);
System.out.println("原始資料:"+lt);
//測試相關方法
//排序[注意:這裡面的Integer等常見的包裝類物件是實現了Comparable自然排序介面,所以可以直接使用]
Collections.sort(lt);
System.out.println("排序完以後:"+lt);
//查詢[注意查詢必須先使用排序,如果說查詢不到元素,那麼返回的是最大索引+1]
System.out.println("查詢指定元素:"+Collections.binarySearch(lt, 100));
System.out.println("查詢指定元素:"+Collections.binarySearch(lt, 10));
//獲取最值
System.out.println("獲取最大值情況:"+Collections.max(lt));
//反轉
Collections.reverse(lt);
System.out.println("反轉以後的引數:"+lt);
//隨機置換
Collections.shuffle(lt);
System.out.println("隨機置換:"+lt);
}
//輸出結果:
原始資料:[12, 10, 17, 14, 13, 16]
排序完以後:[10, 12, 13, 14, 16, 17]
查詢指定元素:-7
查詢指定元素:0
獲取最大值情況:17
反轉以後的引數:[17, 16, 14, 13, 12, 10]
隨機置換:[16, 10, 13, 17, 14, 12]


5.Collections工具類對自定義物件進行排序

(1).使用自然排序[升序排序]
--->實現定義物件Student,並實現介面:Comparable
public class Student implements Comparable<Student>{
//姓名
private String name;
//年齡
private int age;

//設計帶參構造
public Student(String name,int age){
super();
this.age = age;
this.name = name;
}
//省略getXxx()和SetXxx()方法

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

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

--->測試類程式碼
public static void main(String[] args) {
//建立集合物件
List<Student> lt = new ArrayList<Student>();
//建立元素
Student st =new Student("張三", 12);
Student st1 =new Student("李四", 16);
Student st2 =new Student("王五", 11);
Student st3 =new Student("王二麻子", 10);

lt.add(st);
lt.add(st1);
lt.add(st2);
lt.add(st3);

//使用自然排序
Collections.sort(lt);

//遍歷
for(int i = 0;i<lt.size();i++){
System.out.println(lt.get(i).toString());
}
}



(2).使用比較器排序[倒序排序]
---> 實現定義物件Student
public class Student {
//姓名
private String name;
//年齡
private int age;

//設計帶參構造
public Student(String name,int age){
super();
this.age = age;
this.name = name;
}
//省略getXxx()和SetXxx()方法

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

--->測試類程式碼實現
public static void main(String[] args) {
//建立集合物件
List<Student> lt = new ArrayList<Student>();
//建立元素
Student st =new Student("張三", 12);
Student st1 =new Student("李四", 16);
Student st2 =new Student("王五", 11);
Student st3 =new Student("王二麻子", 10);

lt.add(st);
lt.add(st1);
lt.add(st2);
lt.add(st3);

//使用比較器排序[使用匿名物件來實現]
Collections.sort(lt,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(int i = 0;i<lt.size();i++){
System.out.println(lt.get(i).toString());
}

}