Java的中排序方式
生活随笔
收集整理的這篇文章主要介紹了
Java的中排序方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實體類:
public class Person {public String getCode() {return code;}public void setCode(String code) {this.code = code;}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;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Person(String code,String name,int age,String address,String sex){this.code = code;this.name = name;this.age = age;this.address = address;this.sex = sex;}private String code;private String name;private int age;private String address;private String sex; public String toString(){return this.code + "-" + this.name + "-" + this.age + "-" + this.address + "-" +this.sex;} }?
一、Comparator接口排序
import java.util.Comparator;/*** 根據年齡排序* @author wangxc**/ public class ageCompare implements Comparator<Person>{@Overridepublic int compare(Person person1, Person person2) {// TODO Auto-generated method stubreturn (person1.getAge() - person2.getAge());} } import java.util.Comparator;/*** 根據姓名排序* @author wangxc**/ public class nameCompare implements Comparator<Person>{@Overridepublic int compare(Person person1, Person person2) {// TODO Auto-generated method stubreturn person1.getName().compareTo(person2.getName());} }?
import java.util.*;public class Sort{public static void main(String[] args) throws Exception {ArrayList<Person> persons = new ArrayList<Person>();persons.add(new Person("001", "tom", 55, "NewYork","w"));persons.add(new Person("042", "jack", 25, "China","w"));persons.add(new Person("035", "allice", 39, "japan","m"));persons.add(new Person("023", "lili", 21, "England","m"));ageCompare ac = new ageCompare();Collections.sort(persons, ac);System.out.println("sort by age" + persons);nameCompare nc = new nameCompare();Collections.sort(persons, nc);System.out.println("sort by name" + persons);} }?
?二、Comparable接口排序,需要重寫Comparable接口
修改實體類如下圖所示,先根據年齡進行排序,再根據名字長度進行排序:
public class Person implements Comparable<Person>{public String getCode() {return code;}public void setCode(String code) {this.code = code;}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;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Person(String code,String name,int age,String address,String sex){this.code = code;this.name = name;this.age = age;this.address = address;this.sex = sex;}private String code;private String name;private int age;private String address;private String sex; public String toString(){return this.code + "-" + this.name + "-" + this.age + "-" + this.address + "-" +this.sex;}@Overridepublic int compareTo(Person o) {// TODO Auto-generated method stubif (this.age < o.age) { //return -1;}else if(this.age > o.age){return 1;}else {return this.name.compareTo(o.name);}} } import java.util.*;public class hehe {public static void main(String[] args) throws Exception {ArrayList<Person> persons = new ArrayList<Person>();persons.add(new Person("001", "tom", 55, "NewYork","w"));persons.add(new Person("042", "jack", 25, "China","w"));persons.add(new Person("035", "allice", 39, "japan","m"));persons.add(new Person("023", "lili", 21, "England","m"));Collections.sort(persons);System.err.println(persons);} }?
三、ArrayList.sort()? 排序?
實體類無需修改代碼,無需繼承任何接口。
import java.util.*;public class hehe {public static void main(String[] args) throws Exception {ArrayList<Person> persons = new ArrayList<Person>();persons.add(new Person("001", "tom", 55, "NewYork","w"));persons.add(new Person("042", "jack", 25, "China","w"));persons.add(new Person("035", "allice", 39, "japan","m"));persons.add(new Person("023", "lili", 21, "England","m"));persons.sort((p1,p2) -> (p1.getAge() - p2.getAge()));//根據年齡進行排序System.out.println(persons);} }?
四、Comparator.comparing() 排序
import java.util.*;public class hehe {public static void main(String[] args) throws Exception {ArrayList<Person> persons = new ArrayList<Person>();persons.add(new Person("001", "tom", 55, "NewYork","w"));persons.add(new Person("042", "jack", 25, "China","w"));persons.add(new Person("035", "allice", 39, "japan","m"));persons.add(new Person("023", "lili", 21, "England","m"));//先根據年齡排序再根據名字排序最后反轉序列persons.sort(Comparator.comparing(Person::getAge).thenComparing(Person::getName).reversed());System.out.println(persons);} }?
總結
以上是生活随笔為你收集整理的Java的中排序方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode——1721.交换链表中
- 下一篇: org.apache.ibatis.bi