今天就跟大家聊聊有關(guān)Java中怎么實現(xiàn)sort集合排序,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)成立于2013年,先為沭陽等服務建站,沭陽等地企業(yè),進行企業(yè)商務咨詢服務。為沭陽企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
Comparable和Comparator
public staticvoid sort(List list); 將集合中的數(shù)據(jù)按照默認規(guī)則進行排序 (我們在自己的類里面實現(xiàn)Comparabl接口方法compareTo) public static void sort(List list, Comparator super T>); 將集合中的數(shù)據(jù)按照指定規(guī)則進行排序
Comparable
public class Demo02Sort { public static void main(String[] args) { ArrayListobjects = 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 { 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 staticvoid sort(List list, Comparator super T> c) * * Comparable: 我們的類繼承Comparable接口,然后實現(xiàn)里面的CompareTo方法, * 如何調(diào)用:Collections.sort(arrayList) * * Comparator: 我們的類不用繼承Comparable接口,也不用繼承Comparator接口 * 如何調(diào)用:Collections.sort(objects, new Comparator () { // 匿名類 @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 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 () { @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接口,然后實現(xiàn)里面的CompareTo方法, * 如何調(diào)用:Collections.sort(arrayList) * * Comparator: 我們的類不用繼承Comparable接口,也不用繼承Comparator接口 * 如何調(diào)用:Collections.sort(objects, new Comparator() { // 匿名類 @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; } });
看完上述內(nèi)容,你們對Java中怎么實現(xiàn)sort集合排序有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。