介绍
Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作。
方法
排序 sort
使用sort方法可以根据元素的自然顺序 对指定列表按升序进行排序。
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);
Collections.sort(intList);
列表中的所有元素都必须实现 Comparable接口或Comparator。即列表内的所有元素都必须是使用指定比较器可相互比较的
package com.thecodesmaple.example.collection;
public class Person {
private int age;
private String firstName;
private String lastName;
public Person(int age, String firstName, String lastName) {
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return "Age: " + age + " FirstName " + firstName + " LastName: "
+ lastName;
}
}
List<Person> personList = Arrays.asList(new Person(21, "Eric", "W"),
new Person(17, "John", "A"), new Person(28, "Allan", "H"),
new Person(15, "Jonas", "B"));
// [Age: 21 FirstName Eric LastName: W, Age: 17 FirstName John LastName:
// A, Age: 28 FirstName Allan LastName: H, Age: 15 FirstName Jonas
// LastName: B]
System.out.println(personList);
Collections.sort(personList, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
// [Age: 15 FirstName Jonas LastName: B, Age: 17 FirstName John
// LastName: A, Age: 21 FirstName Eric LastName: W, Age: 28 FirstName
// Allan LastName: H]
System.out.println("按照年龄排序后:");
System.out.println(personList);
混排 Shuffling
打乱在一个 List 中可能有的任何排列的踪迹。
即基于随机源的输入重排该 List,这样的排列具有相同的可能性(假设随机源是公正的)
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);
Collections.shuffle(intList);
// 一次测试的结果
// [6, 18, 33, 24, 99, 9]
System.out.println(intList);
反转 reverse
反转列表中的元素顺序
List<String> reverseCandidate = Arrays.asList("A", "B", "C");
Collections.reverse(reverseCandidate);
// [C, B, A]
System.out.println(reverseCandidate);
替换指定 replaceAll
替换集合中元素
List<String> replaceAllCandidate = Arrays.asList("A", "B", "C");
// 将A用Z代替
Collections.replaceAll(replaceAllCandidate, "A", "Z");
// [Z, B, C]
System.out.println(replaceAllCandidate);
替换所有 fill
使用指定元素替换指定列表中所有元素
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<str.length;j++){
li.add(new String(str[j]));
}
Collections.fill(li,"aaa");
for (int i = 0; i < li.size(); i++) {
System.out.println("list[" + i + "]=" + li.get(i));
}
拷贝 copy
用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List至少与源一样长。
List<String> listOne = Arrays.asList("A", "B", "C", "D");
List<String> listTwo = Arrays.asList("X", "Y", "Z");
Collections.copy(listOne, listTwo);
System.out.println(listOne);// [X, Y, Z, D]
System.out.println(listTwo);//[X, Y, Z]
最大值Max 最小值Min
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);
// 99
System.out.println(java.util.Collections.max(intList));
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);
// 6
System.out.println(java.util.Collections.min(intList));