跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

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 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 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 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.javapackage 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.javapackage 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 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() {@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() {@Override public int compare(Student o1, Student o2) {return o1.getAge() - o2.getAge();}});System.out.println("mixAgeStudent = "+mixAgeStudent.toString());}}创建不可变集合unmodifiablleXXX()List list = new ArrayList<>();list.add("SpringBoot课程");list.add("架构课程");list.add("微服务SpringCloud课程"); //设置为只读List集合list = Collections.unmodifiableList(list);System.out.println(list);Set set = new HashSet<>();set.add("Mysql教程");set.add("Linux服务器器教程");set.add("Git教程");//设置为只读Set集合set = Collections.unmodifiableSet(set);System.out.println(set);Map map = new HashMap<>();map.put("key1", "课程1");map.put("key2", "课程2");//设置为只读Map集合map = Collections.unmodifiableMap(map);System.out.println(map);以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:Java后台通过Collections获取list集合中最大数,最小数代码Java Collections集合继承结构图_动力节点Java学院整理JAVA对list集合进行排序Collections.sort() JavaSE的三大接口:Comparator,Comparable和Cloneable详解Java Comparable及Comparator接口区别详解Java Comparable和Comparator对比详解Java 比较接口comparable与comparator区别解析Java Comparable 和 Comparator 的详解及区别浅析Java中comparator接口与Comparable接口的区别对比Java中的Comparable排序接口和Comparator比较器接口java 集合工具类Collections及Comparable和Comparator排序详解

相关文章