Java集合框架Collections原理及用法实例
Collections工具类
Java里关于聚合的工具类,包含有各种有关集合操作的静态多态方法,不能实例化(把构造函数私有化)
public class Collections { // Suppresses default constructor, ensuring non-instantiability. private Collections() { } }
和Collection的区别
- Collection是接口,提供了对集合对象进行基本操作的通用接口方法,List、Set等多种具体的实现类
- Collection是工具类,专门从操作Collection接口实现类里面的元素
常见方法
排序
Sort(List list)
按自然排序的升序排序
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("aaaa"); list.add("zzz"); list.add("gggg"); System.out.println(list); Collections.sort(list); System.out.println(list); }
自定义排序规则
Sort(List list,Comparator c),由Comparator控制排序逻辑
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("aaaa"); list.add("zzz"); list.add("gggg"); System.out.println(list); //默认升序 Collections.sort(list, Comparator.naturalOrder()); System.out.println(list); //降序 Collections.sort(list, Comparator.reverseOrder()); System.out.println(list); }
随机排序
shuffle(List list)
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); list.add("7"); list.add("8"); list.add("9"); list.add("10"); list.add("J"); list.add("Q"); list.add("K"); System.out.println(list); Collections.shuffle(list); System.out.println(list); }
获取最值元素
max(Collection coll)#
默认比较,不适合对象比较
max(Collection coll,Comparator comparator)#
Student.java
package com.cyb.test; class Student { public Student(String name, int age) { this.name = name; this.age = age; } private int age; private String name; public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + '\'' + '}'; } }
Test03.java
package com.cyb.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class test03 { public static void main(String[] args) { List<Student> list = new ArrayList<>(); list.add(new Student("jack", 26)); list.add(new Student("tom", 29)); list.add(new Student("mary", 32)); list.add(new Student("tony", 19)); list.add(new Student("smith", 41)); System.out.println(list); Student maxAgeStudent = Collections.max(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }); System.out.println("maxAgeStudent = "+maxAgeStudent.toString()); Student mixAgeStudent = Collections.min(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }); System.out.println("mixAgeStudent = "+mixAgeStudent.toString()); } }
创建不可变集合unmodifiablleXXX()
List<String> list = new ArrayList<>(); list.add("SpringBoot课程"); list.add("架构课程"); list.add("微服务SpringCloud课程"); //设置为只读List集合 list = Collections.unmodifiableList(list); System.out.println(list); Set<String> set = new HashSet<>(); set.add("Mysql教程"); set.add("Linux服务器器教程"); set.add("Git教程"); //设置为只读Set集合 set = Collections.unmodifiableSet(set); System.out.println(set); Map<String, String> map = new HashMap<>(); map.put("key1", "课程1"); map.put("key2", "课程2"); //设置为只读Map集合 map = Collections.unmodifiableMap(map); System.out.println(map);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)