今天就跟大家聊聊有关Java中怎么实现sort集合排序,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联成立于2013年,先为沭阳等服务建站,沭阳等地企业,进行企业商务咨询服务。为沭阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
Comparable和Comparator
public staticvoid sort(List list); 将集合中的数据按照默认规则进行排序 (我们在自己的类里面实现Comparabl接口方法compareTo) public static void sort(List list, Comparator super T>); 将集合中的数据按照指定规则进行排序
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接口,然后实现里面的CompareTo方法, * 如何调用:Collections.sort(arrayList) * * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 * 如何调用: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接口,然后实现里面的CompareTo方法, * 如何调用:Collections.sort(arrayList) * * Comparator: 我们的类不用继承Comparable接口,也不用继承Comparator接口 * 如何调用: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; } });
看完上述内容,你们对Java中怎么实现sort集合排序有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。