使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
生活随笔
收集整理的這篇文章主要介紹了
使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、實現對象數組排序
(1)方法一,需要排序的對象所屬的類實現Comparable接口,復寫 comparaTo方法?(2)方法二,需要排序的對象所屬的類已經完成無法實現Comparable接口,這種情況用實現Comparator接口,需要自定義排序規則類 復寫compare方法
2、實現List集合排序,和對象數組是一樣的規則,只是最終排序調用的工具類是Collections.sort()方法
直接看代碼:
方法一:學生Student類
<span style="font-size:14px;"><span style="font-size:18px;">package cn.com.lcx.model;public class Student implements Comparable<Student>{private String name;private int age;private double score;public String getName() {return name;}public Student(String name, int age, double score) {super();this.name = name;this.age = age;this.score = score;}public void setName(String name) {this.name = name;}public int getAge() {return age;}/*** 重寫toString()方法,方便打印對象信息*/@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", score=" + score+ "]";}public void setAge(int age) {this.age = age;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}/*** 實現Comparable的compareTo 方法* 1表示大于,-1表示小于,0表示等于* 如果if條件和返回值表示一樣(即if表達式是大于 返回值且是1)那么就是升序,反之則是降序*/@Overridepublic int compareTo(Student o) {/*** 按年齡降序(if條件和返回值相反) 年齡相同 按成績升序(if條件和返回值相同)*/if(this.age> o.age){return -1;}else if(this.age< o.age){return 1;}else{if(this.score> o.score){return 1;}else if(this.score< o.score){return -1;}else{return 0;}}} } </span></span> 方法二:學生排序規則類StudentComparator,方法二適用與早已開發好的類,但是卻沒有實現Comparable接口 <span style="font-size:14px;"><span style="font-size:18px;">package cn.com.lcx.model;import java.util.Comparator;public class StudentComparator implements Comparator<Student>{/*** 實現Comparator的compare 方法* 1表示大于,-1表示小于,0表示等于* 如果if條件和返回值表示一樣(即if表達式是大于 返回值且是1)那么就是升序,反之則是降序*/@Overridepublic int compare(Student o1, Student o2) {/*** 按年齡升序(if條件和返回值相同) 年齡相同 按成績降序(if條件和返回值相反)*/if(o1.getAge()> o1.getAge()){return 1;}else if(o1.getAge()< o1.getAge()){return -1;}else{if(o1.getScore()> o2.getScore()){return -1;}else if(o1.getScore()< o2.getScore()){return 1;}else{return 0;}}} } </span></span> 主方法:StudentSort 類測試對象數組、List集合排序功能 <span style="font-size:14px;"><span style="font-size:18px;">package cn.com.lcx.test;import java.util.Arrays; import java.util.Collections; import java.util.List;import cn.com.lcx.model.Student; import cn.com.lcx.model.StudentComparator;public class StudentSort {/*** @param args*/public static void main(String[] args) {/*** 1、實現對象數組排序* (1)方法一,需要排序的對象所屬的類實現Comparable接口,復寫 comparaTo方法* (2)方法二,需要排序的對象所屬的類已經完成無法實現Comparable接口,* 這種情況用實現Comparator接口,需要自定義排序規則類 復寫compare方法* 最終排序用Arrays.sort方法*/Student[] stuArr = {new Student("lwx-1", 10, 100),new Student("lwx-2", 30, 90),new Student("lwx-3", 30, 95),new Student("lwx-4", 30, 85),new Student("lwx-5", 20, 70),new Student("lwx-6", 40, 60)};//方法一實現Comparable接口System.out.println("實現Comparable接口,數組對象排序前----------");printArr(stuArr);System.out.println("實現Comparable接口,數組對象排序后----------");Arrays.sort(stuArr);printArr(stuArr);//方法二自定義排序規則類實現Comparator接口System.out.println("實現Comparator接口,數組對象排序后----------");Arrays.sort(stuArr,new StudentComparator());printArr(stuArr);/*** 2、實現List集合排序,和對象數組是一樣的規則,只是最終排序調用的工具類是Collections.sort()方法*/Student[] stuArr2 = {new Student("lwx-4", 30, 85),new Student("lwx-5", 20, 70),new Student("lwx-6", 40, 60),new Student("lwx-1", 10, 100),new Student("lwx-2", 30, 90),new Student("lwx-3", 30, 95)};List<Student> stuList = Arrays.asList(stuArr2);//方法一實現Comparable接口System.out.println("實現Comparable接口,List集合排序前----------");printList(stuList);System.out.println("實現Comparable接口,List集合排序后----------");Collections.sort(stuList);printList(stuList);//方法二自定義排序規則類實現Comparator接口System.out.println("實現Comparator接口,List集合排序后----------");Collections.sort(stuList,new StudentComparator());printList(stuList);}/*** 打印對象數組元素* @param objArr*/public static void printArr(Object[] objArr){for(Object obj: objArr){System.out.println(obj);}}/*** 打印List集合中的元素* @param list*/public static void printList(List list){for(Object obj: list){System.out.println(obj);}} }</span></span>驗證結果:
總結
以上是生活随笔為你收集整理的使用Comparable、Comparator接口实现对对象数组、List集合自定义排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3d face paper
- 下一篇: TCP/IP 免费ARP