标签:ons nts turn person src 图片 private array 必须
public static <T> void sort(List<T> list); 将集合中的数据按照默认规则进行排序 (我们在自己的类里面实现Comparabl接口方法compareTo) public static <T> void sort(List<T> list, Comparator<? super T>); 将集合中的数据按照指定规则进行排序
1 /** 2 * @author gw 3 * @date 2019/11/5 0005 下午 22:15 4 */ 5 6 /* 7 * public static<T> void sort(List<T> list); 将集合中的数据按照默认规则进行排序 8 * 9 * 注意: 10 * sort使用前提:被排序的集合里面存储的元素,必须实现Comparable(接口),重写接口中的方法compareTo,定义排序规则 11 * 12 * 升序: this - 参数 13 * return 0;就是按照原来顺序输出 14 * 15 * 16 * */ 17 public class Demo02Sort { 18 public static void main(String[] args) { 19 ArrayList<Person> objects = new ArrayList<>(); 20 objects.add(new Person("小明", 23)); 21 objects.add(new Person("小红", 24)); 22 objects.add(new Person("小绿", 22)); 23 24 System.out.println("objects = " + objects); 25 Collections.sort(objects); 26 System.out.println("objects = " + objects); 27 } 28 } 29 30 31 class Person implements Comparable<Person> { 32 private String name; 33 private int age; 34 35 @Override 36 public int compareTo(Person person) { 37 // return 0; 38 return this.getAge() - person.getAge(); 39 40 } 41 42 @Override 43 public String toString() { 44 return "Person{" + 45 "name=‘" + name + ‘\‘‘ + 46 ", age=" + age + 47 ‘}‘; 48 } 49 50 public Person() { 51 } 52 53 public Person(String name, int age) { 54 this.name = name; 55 this.age = age; 56 } 57 58 public String getName() { 59 return name; 60 } 61 62 public void setName(String name) { 63 this.name = name; 64 } 65 66 public int getAge() { 67 return age; 68 } 69 70 public void setAge(int age) { 71 this.age = age; 72 } 73 }
1 /** 2 * @author gw 3 * @date 2019/11/5 0005 下午 22:40 4 */ 5 6 7 /* 8 * Comparator 9 * Comparable都是接口 10 * 11 * public static <T> void sort(List<T> list, Comparator<? super T> c) 12 * 13 * Comparable: 我们的类继承Comparable接口,然后实现里面的CompareTo方法, 14 * 如何调用:Collections.sort(arrayList) 15 * 16 * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 17 * 如何调用:Collections.sort(objects, new Comparator<Person1>() { // 匿名类 18 @Override 19 public int compare(Person1 person1, Person1 t1) { 20 int res = person1.getAge() - t1.getAge(); 21 // 前面减去后面,升序排列 22 if (res == 0) { 23 return person1.getName().charAt(0) - t1.getName().charAt(0); 24 } 25 return res; 26 } 27 }); 28 * 29 * */ 30 public class Demo03SortComparator { 31 public static void main(String[] args) { 32 ArrayList<Person1> objects = new ArrayList<>(); 33 objects.add(new Person1("b小明", 23)); 34 objects.add(new Person1("小红", 24)); 35 objects.add(new Person1("小绿", 22)); 36 objects.add(new Person1("a小绿", 23)); 37 38 System.out.println("objects = " + objects); 39 Collections.sort(objects, new Comparator<Person1>() { 40 @Override 41 public int compare(Person1 person1, Person1 t1) { 42 // return 0; 43 int res = person1.getAge() - t1.getAge(); 44 45 // 前面减去后面,升序排列 46 if (res == 0) { 47 return person1.getName().charAt(0) - t1.getName().charAt(0); 48 } 49 return res; 50 } 51 }); 52 System.out.println("objects = " + objects); 53 } 54 } 55 56 57 class Person1 { 58 private String name; 59 private int age; 60 61 62 @Override 63 public String toString() { 64 return "Person{" + 65 "name=‘" + name + ‘\‘‘ + 66 ", age=" + age + 67 ‘}‘; 68 } 69 70 public Person1() { 71 } 72 73 public Person1(String name, int age) { 74 this.name = name; 75 this.age = age; 76 } 77 78 public String getName() { 79 return name; 80 } 81 82 public void setName(String name) { 83 this.name = name; 84 } 85 86 public int getAge() { 87 return age; 88 } 89 90 public void setAge(int age) { 91 this.age = age; 92 } 93 }
1 * Comparable: 我们的类继承Comparable接口,然后实现里面的CompareTo方法, 2 * 如何调用:Collections.sort(arrayList) 3 * 4 * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 5 * 如何调用:Collections.sort(objects, new Comparator<Person1>() { // 匿名类 6 @Override 7 public int compare(Person1 person1, Person1 t1) { 8 int res = person1.getAge() - t1.getAge(); 9 // 前面减去后面,升序排列 10 if (res == 0) { 11 return person1.getName().charAt(0) - t1.getName().charAt(0); 12 } 13 return res; 14 } 15 });
标签:ons nts turn person src 图片 private array 必须
原文地址:https://www.cnblogs.com/biggw/p/11802422.html