java對ArrayList中元素進行排序的幾種方式總結
更新時間:2024年08月19日 08:57:20 作者:Ez4Sterben
在Java中,ArrayList類提供了多種排序方法,可以根據不同的需求選擇適合的排序方法,下面這篇文章主要給大家介紹了關于java對ArrayList中元素進行排序的幾種方式,需要的朋友可以參考下
一、使用Collections工具類
1、對基本類型排序
通過Collections.sort()對基本類型排序默認是以升序排序
// 1.Collections.sort()默認按照升序排序 List<Integer> integerList = new ArrayList<>(); Collections.addAll(integerList,1,2,6,5,5,4,55,4,5,5,4,5,2,4,6,2,45); Collections.sort(integerList); System.out.println(integerList);
2、對字符串類型排序
對字符串類型排序默認按照首字母a-z排序
// 2.對字符串類型排序 List<String> strings = new ArrayList<>(); Collections.addAll(strings,"d","gsf","trh","fsd","an"); Collections.sort(strings); System.out.println(strings);
3、對對象排序
如何使用Collections對對象排序呢?

其實只需要讓我們的數據類型實現Comparable接口即可,下面定義一個實現Comparable接口的學生類,并且實現compareTo方法,讓學生類只比較年齡。
/**
* 學生
*
* @author ez4sterben
* @date 2023/07/18
*/
public class Student implements Comparable<Student> {
private String id;
private String name;
private Integer age;
private String sex;
@Override
public String toString() {
return "Student{" +
"age=" + age +
'}';
}
public Student(Integer age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public int compareTo(Student o) {
// 這種是升序
return this.getAge() - o.getAge();
// 這種是降序
// return o.getAge() - this.getAge();
}
}
排序方法和正常使用一樣,直接把整個list傳入即可
// 3.對對象排序
List<Student> students = new ArrayList<>();
Collections.addAll(students,
new Student(18),
new Student(26),
new Student(20),
new Student(16),
new Student(12));
System.out.println(students);
Collections.sort(students);
System.out.println(students);

二、使用stream流
// 2.lambda表達式
Stream<Integer> sorted = integerList.stream().sorted();
System.out.println(Arrays.toString(sorted.toArray()));

三、使用排序算法(以冒泡排序為例)
// 3.直接使用排序算法(以冒泡排序為例)
for(int i = 0; i < integerList.size() - 1; i++){
for(int j = 0; j < integerList.size() - i - 1; j++){
if(integerList.get(j) > integerList.get(j + 1)){
Integer temp = integerList.get(j);
integerList.set(j, integerList.get(j + 1));
integerList.set(j + 1, temp);
}
}
}
System.out.println(integerList);

四、測試類整體代碼
import java.util.*;
import java.util.stream.Stream;
/**
* 數組列表排序
*
* @author ez4sterben
* @date 2023/07/19
*/
public class ArrayListSort {
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
Collections.addAll(integerList,1,2,6,5,5,4,55,4,5,5,4,5,2,4,6,2,45);
// 1.Collections.sort()默認按照升序排序
Collections.sort(integerList);
System.out.println(integerList);
// 2.對字符串類型排序
List<String> strings = new ArrayList<>();
Collections.addAll(strings,"d","gsf","trh","fsd","an");
Collections.sort(strings);
System.out.println(strings);
// 3.對對象排序
List<Student> students = new ArrayList<>();
Collections.addAll(students,
new Student(18),
new Student(26),
new Student(20),
new Student(16),
new Student(12));
System.out.println(students);
Collections.sort(students);
System.out.println(students);
// 2.lambda表達式
Stream<Integer> sorted = integerList.stream().sorted();
System.out.println(Arrays.toString(sorted.toArray()));
// 3.直接使用排序算法(以冒泡排序為例)
for(int i = 0; i < integerList.size() - 1; i++){
for(int j = 0; j < integerList.size() - i - 1; j++){
if(integerList.get(j) > integerList.get(j + 1)){
Integer temp = integerList.get(j);
integerList.set(j, integerList.get(j + 1));
integerList.set(j + 1, temp);
}
}
}
System.out.println(integerList);
}
}總結
到此這篇關于java對ArrayList中元素進行排序的幾種方式總結的文章就介紹到這了,更多相關java對ArrayList元素排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring boot工具類靜態(tài)屬性注入及多環(huán)境配置詳解
這篇文章主要為大家詳細介紹了Spring boot工具類靜態(tài)屬性注入,及多環(huán)境配置詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Spring?Boot?整合?Fisco?Bcos的案例分析(區(qū)塊鏈)
本篇文章介紹的?Spring?Boot?整合?Fisco?Bcos的案例,是在阿里云服務器上部署驗證的。大家可根據自己的電腦環(huán)境,對比該案例進行開發(fā)即可,具體案例代碼跟隨小編一起看看吧2022-01-01

