标签:ural compare 排序 基本操作 bsp cti out min 课程
public class Collections { // Suppresses default constructor, ensuring non-instantiability. private Collections() { } }
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); }
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); }
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); }
默认比较,不适合对象比较
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()); } }
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);
标签:ural compare 排序 基本操作 bsp cti out min 课程
原文地址:https://www.cnblogs.com/chenyanbin/p/13401255.html