這篇文章主要介紹了java操作集合工具類Collections使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Collections是一個操作Set、List和Map等集合的工具類。
Collections中提供了大量方法對集合元素進行排序、查詢和修改等操作,還提供了對集合對象設置不可變、對集合對象實現同步控制等方法。
排序操作:
package collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(1);
list1.add(4);
list1.add(3);
System.out.println("原始的list1:"+list1);
Collections.reverse(list1);
System.out.println("反轉之后的list1:"+list1);
Collections.sort(list1);
System.out.println("排序之后的list1:"+list1);
Collections.shuffle(list1);
System.out.println("打亂之后的list1:"+list1);
List<Student> list2 = new ArrayList<Student>();
Student s1 = new Student("tom",21);
Student s2 = new Student("jack",16);
Student s3 = new Student("bob",32);
list2.add(s1);
list2.add(s2);
list2.add(s3);
Collections.sort(list2, new Student());
System.out.println("按照年齡降序進行排序:");
for(Student stu:list2) {
System.out.println("name="+stu.name+","+"age="+stu.age);
}
}
}
class Student implements Comparator<Student>{
String name;
int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.age>o2.age) {
return -1;
}else if (o1.age<o2.age) {
return 1;
}else {
return 0;
}
}
}
輸出:
原始的list1:[2, 1, 4, 3] 反轉之后的list1:[3, 4, 1, 2] 排序之后的list1:[1, 2, 3, 4] 打亂之后的list1:[1, 3, 2, 4] 按照年齡降序進行排序: name=bob,age=32 name=tom,age=21 name=jack,age=16
查找、替換操作:
package collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(1);
list1.add(4);
list1.add(3);
list1.add(1);
System.out.println(list1);
System.out.println(Collections.max(list1));
System.out.println(Collections.min(list1));
System.out.println(Collections.frequency(list1,1));
System.out.println(Collections.replaceAll(list1, 1,2));
System.out.println(list1);
List<Student> list2 = new ArrayList<Student>();
Student s1 = new Student("tom",21);
Student s2 = new Student("jack",16);
Student s3 = new Student("bob",32);
list2.add(s1);
list2.add(s2);
list2.add(s3);
for(Student stu:list2) {
System.out.println("name="+stu.name+","+"age="+stu.age);
}
System.out.println(Collections.max(list2,new Student()).name
+"/"+Collections.max(list2,new Student()).age);
System.out.println(Collections.min(list2,new Student()).name
+"/"+Collections.min(list2,new Student()).age);
// Collections.sort(list2, new Student());
// System.out.println("按照年齡降序進行排序:");
// for(Student stu:list2) {
// System.out.println("name="+stu.name+","+"age="+stu.age);
// }
}
}
class Student implements Comparator<Student>{
String name;
int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.age>o2.age) {
return 1;
}else if (o1.age<o2.age) {
return -1;
}else {
return 0;
}
}
}
輸出:
[2, 1, 4, 3, 1] 4 1 2 true [2, 2, 4, 3, 2] name=tom,age=21 name=jack,age=16 name=bob,age=32 bob/32 jack/16
同步控制:Collections中提供了多個synchronizedXxx()方法,該方法可以使指定集合包裝成線程同步的集合,從而可以解決多線程并發訪問集合時線程安全問題。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。