Home > Java > javaTutorial > body text

How to use Records class in Java 14 for automatic comparison and sorting

WBOY
Release: 2023-07-30 13:06:20
Original
1198 people have browsed it

How to use the Records class in Java 14 to implement automatic comparison and sorting

Java 14 introduces a new class called the Records class, which provides us with a concise and powerful way to Define immutable data classes. The Records class has the feature of automatically generating getter methods, equals() methods and hashCode() methods for each field, which makes comparison and sorting very convenient. In this article, we will demonstrate through sample code how to use Records class in Java 14 to implement automatic comparison and sorting.

The first step is to define a Records class. Let's take the student class as an example. Suppose a student has three fields: name, age and grades. You can define a Records class as follows:

public record Student(String name, int age, double score) {}
Copy after login

In this example, we use the record keyword to define a Records class named Student and specify three fields: name, age, and score. All fields of the Records class will automatically generate corresponding getter methods.

Next, we can create some Student objects and compare and sort them. The following is a sample code:

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

public class Main {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 20, 85.0));
        students.add(new Student("Bob", 19, 90.0));
        students.add(new Student("Charlie", 21, 80.0));

        // 自动比较和排序
        Collections.sort(students);

        for (Student student : students) {
            System.out.println(student);
        }
    }
}
Copy after login

In this example, we create a List named students, which contains three Student objects. Then, we use the Collections.sort() method to sort this List. Since the Student class is a Records class, it automatically implements the Comparable interface, so comparison and sorting are performed by calling the compareTo() method of this interface.

Finally, we can print the sorted results by traversing the students list. Since the Student class automatically generates the toString() method, we can directly use System.out.println() to print the Student object.

The output results are as follows:

Student[name=Alice, age=20, score=85.0]
Student[name=Bob, age=19, score=90.0]
Student[name=Charlie, age=21, score=80.0]
Copy after login

As we can see, the Student objects have been sorted from high to low according to their grades.

In this example, we show how to use the Records class in Java 14 to implement automatic comparison and sorting. The Records class provides us with a concise way to define immutable data classes and automatically generates getter methods, equals() methods and hashCode() methods for each field. By simply implementing the Comparable interface, we can easily perform automatic comparison and sorting. This provides Java programmers with a more efficient and convenient way of operation.

The above is the detailed content of How to use Records class in Java 14 for automatic comparison and sorting. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!