Java sort集合排序的两种方式解析
人气:0这篇文章主要介绍了Java sort集合排序的两种方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Comparable和Comparator
public static <T> void sort(List<T> list); 将集合中的数据按照默认规则进行排序 (我们在自己的类里面实现Comparabl接口方法compareTo) public static <T> void sort(List<T> list, Comparator<? super T>); 将集合中的数据按照指定规则进行排序
Comparable
/** * @author gw * @date 2019/11/5 0005 下午 22:15 */ /* * public static<T> void sort(List<T> list); 将集合中的数据按照默认规则进行排序 * * 注意: * sort使用前提:被排序的集合里面存储的元素,必须实现Comparable(接口),重写接口中的方法compareTo,定义排序规则 * * 升序: this - 参数 * return 0;就是按照原来顺序输出 * * * */ public class Demo02Sort { public static void main(String[] args) { ArrayList<Person> objects = new ArrayList<>(); objects.add(new Person("小明", 23)); objects.add(new Person("小红", 24)); objects.add(new Person("小绿", 22)); System.out.println("objects = " + objects); Collections.sort(objects); System.out.println("objects = " + objects); } } class Person implements Comparable<Person> { private String name; private int age; @Override public int compareTo(Person person) { // return 0; return this.getAge() - person.getAge(); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Comparator
/** * @author gw * @date 2019/11/5 0005 下午 22:40 */ /* * Comparator * Comparable都是接口 * * public static <T> void sort(List<T> list, Comparator<? super T> c) * * Comparable: 我们的类继承Comparable接口,然后实现里面的CompareTo方法, * 如何调用:Collections.sort(arrayList) * * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 * 如何调用:Collections.sort(objects, new Comparator<Person1>() { // 匿名类 @Override public int compare(Person1 person1, Person1 t1) { int res = person1.getAge() - t1.getAge(); // 前面减去后面,升序排列 if (res == 0) { return person1.getName().charAt(0) - t1.getName().charAt(0); } return res; } }); * * */ public class Demo03SortComparator { public static void main(String[] args) { ArrayList<Person1> objects = new ArrayList<>(); objects.add(new Person1("b小明", 23)); objects.add(new Person1("小红", 24)); objects.add(new Person1("小绿", 22)); objects.add(new Person1("a小绿", 23)); System.out.println("objects = " + objects); Collections.sort(objects, new Comparator<Person1>() { @Override public int compare(Person1 person1, Person1 t1) { // return 0; int res = person1.getAge() - t1.getAge(); // 前面减去后面,升序排列 if (res == 0) { return person1.getName().charAt(0) - t1.getName().charAt(0); } return res; } }); System.out.println("objects = " + objects); } } class Person1 { private String name; private int age; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public Person1() { } public Person1(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
* Comparable: 我们的类继承Comparable接口,然后实现里面的CompareTo方法, * 如何调用:Collections.sort(arrayList) * * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 * 如何调用:Collections.sort(objects, new Comparator<Person1>() { // 匿名类 @Override public int compare(Person1 person1, Person1 t1) { int res = person1.getAge() - t1.getAge(); // 前面减去后面,升序排列 if (res == 0) { return person1.getName().charAt(0) - t1.getName().charAt(0); } return res; } });
您可能感兴趣的文章:
加载全部内容