Java advanced---Collections class

Java Advanced-Collections Class

CollectionsClass andCollectionInterfaces are completely different things, everyone must be clear.
The Collections class is the same as the Arrays class mentioned earlier, which is a tool class. One is for collection operations and the other is for arrays.

1. Commonly used methods in the Collections class:

1.swap(List list, int i, int j): swap the positions of the two indexes in the specified list

2.sort(List list): Sort according to the natural order of the elements in the list

3.shuffle(List list): random replacement

4.reverse(List list): reverse

5.fill(List list, Object obj): Use the specified object to fill all elements of the specified list

6.copy(List dest, List src): is to overwrite the data in the source list to the target list

7.binarySearch(List list, Object key) Use binary search to find the index position of the specified element in the specified list

8.max()\min(): find the maximum\minimum value

Here we look at the source code of the sort of Collections
[Note: the sort in the Collections class is actually the sort in the Arrays class]
Click on Collections.sort to see it
Insert picture description here
and then click in. Pay attention: first convert to an object array, and then use Arrays .sort to sort, did you suddenly find something
Insert picture description here
--------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------

Two, two comparison methods

When we want to make a custom comparison, the following two methods will be used.
Insert picture description here
1. UseComparatorCompare (not recommended)
rewritecomparemethod

Implementation:

ArrayList<Integer> list = new ArrayList();
list.add(49);
list.add(29);
list.add(88);
list.add(-49);
list.add(56);

System.out.println(list);
Collections.sort(list);     //默认升序
System.out.println(list);

Collections.sort(list, new Comparator<Integer>() {	//这里就是匿名内部类,重写了compare方法
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;       //降序:o2-o1,升序:o1-o2
    }
});
System.out.println(list);

Why is it not recommended? The reason is that the compare method has to be rewritten for every comparison, which is troublesome

2. UseComparableCompare (recommended)
rewritecompareTomethod

Requirement: Compare and sort according to students' grades.
Specific implementation:
Student class:

public class Student  implements Comparable<Student>{
    private String name;
    private String gender;
    private int score;

    public Student(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    public Student(String name, String gender, int score) {
        this.name = name;
        this.gender = gender;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public double getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return  "姓名:" + name + "\t" +
                "性别:" + gender + "\t"+
                "成绩:" + score ;
    }

    @Override
    public int compareTo(Student o) {		//重写compareTo方法
        return this.score-o.score;			//成绩升序
    }
}

Test category:

Student s1 = new Student("Jack","男",98);
Student s2 = new Student("Alis","女",87);
Student s3 = new Student("Louis","女",59);
Student s4 = new Student("Tom","男",88);

List<Student> list = new LinkedList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);

Collections.sort(list);
System.out.println(list);

The results show
Insert picture description here
that it is more convenient to write once and share all classes.

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/113194379