Linked lists and sorting by a certain amount (Collection.sort(list);)

Linked list

eg: known

class Student {
    
    
	int id;int age;String name;
	Student(int id,int age,String name){
    
    
		this.id=id;
		this.age=age;
		this.name=name;
	}
}

Create a linked list of Student type objects.

1. Import the package:
import java.util.ArrayList;
import java.util.List;
2. Statement:
List list=new ArrayList<>();
3. Add elements to the linked list:
list.add(new Student(-1,25,"关羽"));
list.add(new Student(3,21,"Zhang Fei"));
list.add(new Student(2,18,"刘备" ));
list.add(new Student
(4,32,"袁绍")); list.add(new Student(-2,16,"曹操"));
4. Output the id of all elements in the linked list:
for(Student student:list) {
System.out.printf("%d ",student.id);
}
5. If you want to output all the information, add a method to the Student class:
public String toString() {
return name +",id=" + id +", age=" + age ;
}

Code:

import java.util.ArrayList;
import java.util.List;

public class Main{
    
    
	public static void main(String args[]) {
    
    
		List<Student> list=new ArrayList<>();
		list.add(new Student(-1,20,"赵今今"));
		list.add(new Student(3,21,"陆峥"));
		list.add(new Student(2,18,"庞贝"));
		list.add(new Student(4,24,"喻幸"));
		list.add(new Student(-2,18,"苏盛夏"));
		list.add(new Student(5,22,"冷夜辰"));
		System.out.println("排序前:");
		for(Student student:list) {
    
    
			System.out.printf("%d ",student.id);
		}
		System.out.println();
		System.out.println();
		for(Student student:list) {
    
    
			System.out.println(student.toString());
		}
	}
}
class Student implements Comparable<Student>{
    
    
	int id;int age;String name;
	Student(int id,int age,String name){
    
    
		this.id=id;
		this.age=age;
		this.name=name;
	}
	@Override
	public int compareTo(Student o) {
    
    
		// TODO 自动生成的方法存根
		return this.id-o.id;
	}
	public String toString() {
    
    
        return name +",id=" + id +", age=" + age ;
    }
}

result:
Insert picture description here


Sort by a certain amount (Collection.sort(list);)

eg: Sort and output students by id.

On the basis of the above linked list:

1. Import the package:
import java.util.Collections;
2. Implement the interface:
class Student implements Comparable<Student>
3. Rewrite the method in the interface:
public int compareTo(Student o) {
return this.id-o.id;
}//sort the output according to id, if it is output according to other quantity, just change it
4. Call method:
Collections.sort(list);
5. Output information of all Student type objects in the linked list:
for(Student student:list) {
System.out.println(student.toString());
}

code show as below:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main{
    
    
	public static void main(String args[]) {
    
    
		List<Student> list=new ArrayList<>();
		list.add(new Student(-1,20,"赵今今"));
		list.add(new Student(3,21,"陆峥"));
		list.add(new Student(2,18,"庞贝"));
		list.add(new Student(4,24,"喻幸"));
		list.add(new Student(-2,18,"苏盛夏"));
		list.add(new Student(5,22,"冷夜辰"));
		System.out.println("排序前:");
		for(Student student:list) {
    
    
			System.out.printf("%d ",student.id);
		}
		System.out.println();
		for(Student student:list) {
    
    
			System.out.println(student.toString());
		}
		Collections.sort(list);
		System.out.println();
		System.out.println("排序后:");
		for(Student student:list) {
    
    
			System.out.printf("%d ",student.id);
		}
		System.out.println();
		for(Student student:list) {
    
    
			System.out.println(student.toString());
		}
	}
}
class Student implements Comparable<Student>{
    
    
	int id;int age;String name;
	Student(int id,int age,String name){
    
    
		this.id=id;
		this.age=age;
		this.name=name;
	}
	@Override
	public int compareTo(Student o) {
    
    
		// TODO 自动生成的方法存根
		return this.id-o.id;
	}
	public String toString() {
    
    
        return name +",id=" + id +", age=" + age ;
    }
}

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46020391/article/details/112994518