Java集合框架Collections原理及用法实例
时间:2020-10-15 23:16:47|栏目:JAVA代码|点击: 次
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);
栏 目:JAVA代码
下一篇:详解Spring Cloud Stream使用延迟消息实现定时任务(RabbitMQ)
本文标题:Java集合框架Collections原理及用法实例
本文地址:http://www.codeinn.net/misctech/11823.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机