java教程之二個arraylist排序的示例分享
示例1
package com.yonyou.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
Student zlj = new Student("丁曉宇", 21);
Student dxy = new Student("趙四", 22);
Student cjc = new Student("張三", 11);
Student lgc = new Student("劉武", 19);
List<Student> studentList = new ArrayList<Student>();
studentList.add(zlj);
studentList.add(dxy);
studentList.add(cjc);
studentList.add(lgc);
System.out.println("按照年齡排序:");
Collections.sort(studentList, new SortByAge());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
}
System.out.println(" ========= ");
System.out.println("按照姓名排序");
Collections.sort(studentList, new SortByName());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
}
}
}
class SortByAge implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.getAge() > s2.getAge())
return 1;
else if (s1.getAge() == s2.getAge()) {
return 0;
}
return -1;
}
}
class SortByName implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if(s1.getName().compareTo(s2.getName()) < 0)
return -1;
else if (s1.getName().compareTo(s2.getName()) > 0) {
return 1;
}
return 0;
}
}
class Student{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name,int age) {
this.age = age;
this.name = name;
}
}
示例2
常常遇到數(shù)組排序的問題.比如我有一個Person類,它的實例對象存儲在ArrayList數(shù)組中,現(xiàn)在要把ArrayList數(shù)組中 的Person對象按照年齡排序.
其實這種情況經(jīng)常遇到.
下面給出源代碼:
1:Person.java文 件:
public class Person{
String name;
int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2:Mycomparator.java
// 實現(xiàn)Comparator接口,也就是定義排序規(guī)則,你幾乎可以定義任何規(guī)則
package com.infoearth;
import java.util.*;
public class Mycomparator implements Comparator{
public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
if(p1.age<p2.age)
return 1;
else
return 0;
}
}
3:ListSort.java
package com.infoearth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ListSort {
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(new Person("lcl",28));
list.add(new Person("fx",23));
list.add(new Person("wqx",29));
Comparator comp = new Mycomparator();
Collections.sort(list,comp);
for(int i = 0;i<list.size();i++){
Person p = (Person)list.get(i);
System.out.println(p.getName());
}
}
}
當然,如果你的List包裝的是基本類型或者String,則只要 Collections.sort(list);即可
相關(guān)文章
Java 日期格式y(tǒng)yyy-MM-dd與YYYY-MM-dd區(qū)別
我們在java中常用的規(guī)范格式為:2023-11-11
yyyy-MM-dd HH:mm:ss:SSS 24小時制或yyyy-MM-dd hh:mm:ss:SSS 12小時制,本文就來介紹一下兩者的區(qū)別,感興趣的可以了解一下
SpringBoot中使用Redis對接口進行限流的實現(xiàn)
本文將結(jié)合實例代碼,介紹SpringBoot中使用Redis對接口進行限流的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
SpringBoot整合Netty服務(wù)端的實現(xiàn)示例
Netty提供了一套完整的API,用于處理網(wǎng)絡(luò)IO操作,如TCP和UDP套接字,本文主要介紹了SpringBoot整合Netty服務(wù)端的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-07-07
knife4j+springboot3.4異常無法正確展示文檔
本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2025-01-01
hibernate-validator后端表單數(shù)據(jù)校驗的使用示例詳解
這篇文章主要介紹了hibernate-validator后端表單數(shù)據(jù)校驗的使用,hibernate-validator提供的校驗方式為在類的屬性上加入相應(yīng)的注解來達到校驗的目的,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-08-08

