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!