Home> Java> javaTutorial> body text

Pattern Matching and Records Changes in Java Every Java Developer Must Know

王林
Release: 2024-08-12 06:47:32
Original
630 people have browsed it

Pattern Matching and Records Changes in Java  Every Java Developer Must Know

With the release of Java 16, a major improvement was introduced with the introduction of Records (JEP 395), which allowed for a simpler and more concise way to declare classes that are primarily used to carry data. This improvement has now been further enhanced in Java 21, with the addition of Pattern Matching and Records (JEP 406). This new feature allows for the use of pattern matching to test whether a value is an instance of a Record class and extract its components in a more streamlined way. In this article, we will explore the changes brought about by Pattern Matching and Records in Java 21 and how it can benefit Java developers.

Records as Transparent Carriers for Data
Records, introduced in Java 16, are classes that are primarily used to store and carry data. They are transparent carriers, meaning that their main purpose is to hold data, and all other functionality such as constructors, methods, and equals/hashCode methods, are generated automatically by the compiler, based on the data fields defined in the record. This makes them ideal for use in scenarios where data needs to be serialized or sent over the network.

Consider the example of a Line class that defines two X and Y coordinates:

record Line(int x, int y) {}

To use this class, we can simply create an instance of the Line class and access its data fields using the built-in component accessor methods, x() and y():

Line line = new Line(0, 10);
int x = line.x();
int y = line.y();
System.out.println("X: " + x + ", Y: " + y); // Output: X: 0, Y: 10

Pattern Matching with Records
In Java 21, Pattern Matching has been added which makes it possible to test whether a value is an instance of a Record class and extract its components in a more streamlined way. This feature is especially useful when dealing with large codebases that use Records extensively.

Consider the following example where we want to test whether an object is an instance of the Line class and extract its components:

static void length(Object obj) {
if (obj instanceof Line l) {
int x = l.x();
int y = l.y();
System.out.println(y-x);
}
}
As you can see, we have used a type pattern to test whether the object is an instance of Point and if so, we have extracted its components by invoking the built-in component accessor methods. While this code works, it can be further simplified with the use of a record pattern in Java 21.

With record pattern, we can not only test whether a value is an instance of a Record class, but we can also extract its components in a single line of code. This is achieved by lifting the declaration of local variables for the extracted components into the pattern itself, and initializing those variables by invoking the accessor methods when the value is matched against the pattern.

Consider the following code that uses a record pattern:

static void length(Object obj) {
if (obj instanceof Line(int x, int y)) {
System.out.println(y-x);
}
}
This code is much more concise and readable. We have eliminated the need for creating a new object and invoking its component accessor methods to get the data. The record pattern directly extracts and initializes the components for us, making our code more streamlined.

Nested Record Patterns
One of the major challenges faced by developers is dealing with complex object graphs and extracting data from them. This is where the real power of pattern matching comes in, as it allows us to scale elegantly and match more complicated object graphs.

Consider the following classes: Employee, Department (enum), and Company (record). We can use a record pattern to extract the Department of an Employee from a Company object:

// As of Java 21
static void printEmployeeDepartment(Company c, String name) {
if (c instanceof Company(Department dept, List employees)) {
for (Employee e : employees) {
if (e.getName().equals(name)) {
System.out.println(name + " is in " + dept + " department.");
return;
}
}
}
System.out.println(name + " not found.");
}
In this example, we are using nested patterns to extract the Department of an Employee from a Company object. We check if the given Company object has a Department and a list of Employees, and then loop through the list to find the Employee with the given name. If the Employee is found, we print their department. If not, we print a message saying that the Employee was not found.

ネストされたパターンは、複数の値を一度に照合して分解する必要がある状況でも使用できます。指定された座標が四角形の内側にあるかどうかを確認する次の例を考えてみましょう:

//Java 21 以降
レコードポイント(double x, double y) {}

長方形を記録(左上点、右下点) {}

// 指定された点が指定された四角形の内側にあるかどうかを確認します
static boolean isPointInsideRectangle(Point p, Rectangle r) {
if (r インスタンスオブ Rectangle(Point(var x1, var y1), Point(var x2, var y2))) {
if (p.x() > x1 && p.y() > y1 && p.x() true を返します;
}
}
false を返します;
}

この例では、ネストされたパターンを使用して、指定された Point オブジェクトが指定された Rectangle オブジェクトの境界内にあるかどうかを確認します。ネストされたパターンを使用すると、複数行のコードを記述することなく、長方形の左上と右下の点の x 座標と y 座標にアクセスできます。

結論として、Java 21 にパターン マッチングとレコード (JEP 406) が追加されたことで、複雑なオブジェクトからデータを処理および抽出する方法が大幅に改善されました。この機能によりコードが大幅に簡素化され、より読みやすく簡潔になります。また、パターン一致が失敗する可能性がある失敗シナリオの処理にも役立ちます。これらの変更により、Java 21 では引き続きコードがより合理化され、Java 開発者の開発エクスペリエンスが向上します。

MyExamCloud の Java SE 21 Developer Professional Practice Tests で Java 21 スキルをアップグレードします。 Java 21 エキスパートになるための知識を開発し、テストしてください。

The above is the detailed content of Pattern Matching and Records Changes in Java Every Java Developer Must Know. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
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!