[java] Several ways to sort the elements in ArrayList

Several ways to sort elements in ArrayList

1. Use the Collections tool class

1. Sort the basic types

Sorting by Collections.sort()primitive type defaults to ascending order

// 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. Sorting string types

Sorting string types is sorted by the first letter az by default

// 2.对字符串类型排序
List<String> strings = new ArrayList<>();
Collections.addAll(strings,"d","gsf","trh","fsd","an");
Collections.sort(strings);
System.out.println(strings);

3. Sort objects

How to use Collections to sort objects?

insert image description here

In fact, we only need to let our data type implement the Comparable interface. Next, define a student class that implements the Comparable interface, and implement the compareTo method, so that the student class only compares age.

/**
 * 学生
 *
 * @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();
    }
}

The sorting method is the same as normal use, just pass in the entire list directly

// 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);

insert image description here

Two, use the stream flow

// 2.lambda表达式
        Stream<Integer> sorted = integerList.stream().sorted();
        System.out.println(Arrays.toString(sorted.toArray()));

insert image description here

3. Use sorting algorithm (take bubble sort as an example)

// 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);

insert image description here

Fourth, the overall code of the test class

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);

    }
}

Guess you like

Origin blog.csdn.net/qq_51383106/article/details/131814255