Now Hiring: Are you an experienced and enthusiastic java developer?

Blog

Java 20 : Record Patterns (Second Preview)

record-patterns-in-java-20
Java

Java 20 : Record Patterns (Second Preview)

Originally, the Record patterns feature was introduced in Java 19, that allow you to match values against a record type and bind variables to the corresponding components of the record. Record patterns can be used in conjunction with type patterns to “enable a robust, declarative, and composable form of data navigation and processing.”To use a record pattern, you first need to create a record type. Record types are a new feature in Java 10 that provide a concise and easy way to create immutable data structures. To create a record type, you use the record keyword followed by the name of the record type and a list of the record’s components.

For example, the following code creates a record type called Point with two components, x and y:

record Point(int x, int y) {}

Once you have created a record type, you can use a record pattern to match values against that type and bind variables to the corresponding components of the record.

For example, the following code uses a record pattern to match a value of type Point and bind the x and y components to the variables x and y:

Point p = new Point(10, 20);

// Match the value of p against the record pattern Point(var x, var y)
if (p match Point(var x, var y)) {
  System.out.println("x = " + x);
  System.out.println("y = " + y);
}

This code will print the following output:

x = 10
y = 20

Record patterns, which are on second preview in Java 20, allow you to dissect record values. This can be used to make your code more understandable and concise.
For example, the following code deconstructs a record value using a record pattern:

record Person(String name, int age) { }

Person person = new Person("John Doe", 30);

// Deconstruct the record value
var name = person.name;
var age = person.age;

This can be written more concisely using a record pattern:

var person = new Person("John Doe", 30);

var name = person.name;
var age = person.age;

Record patterns can also be utilised in expressions and switch statements. The following code, for example, use a record pattern to identify the suitable case statement:

switch (person) {
  case Person("John Doe", 30):
    System.out.println("John Doe is 30 years old");
    break;
  case Person("Jane Doe", 25):
    System.out.println("Jane Doe is 25 years old");
    break;
  default:
    System.out.println("Unknown person");
    break;
}

This can be written more concisely using a record pattern:

switch (person) {
  case Person("John Doe", 30):
  case Person("Jane Doe", 25):
    System.out.println(person.name + " is " + person.age + " years old");
    break;
  default:
    System.out.println("Unknown person");
    break;
}

Record patterns are a strong new feature that can help you write more compact and readable code. They can also be used to improve your code’s readability and maintainability.
Here are some additional examples of how to use record patterns:

  • To filter a list of records:
List<Person> people = Arrays.asList(
  new Person("John Doe", 30),
  new Person("Jane Doe", 25),
  new Person("Bill Smith", 40)
);

List<Person> adults = people.stream()
  .filter(person -> person.age >= 18)
  .collect(Collectors.toList());
  • To create a new record from an existing record:
Person person = new Person("John Doe", 30);

Person newPerson = new Person(person.name, person.age + 1);
  • To map a record to another type:
Map<String, Integer> ages = people.stream()
  .collect(Collectors.toMap(person -> person.name, person -> person.age));

Record patterns are a strong new feature that can help you write more concise, readable, and maintainable code. They are an important addition to the Java language and can assist you in writing better code.

Conclusion

In conclusion, record patterns are a valuable addition to the Java language, introduced in Java 20(second preview), that allow for the matching, deconstruction, and processing of record values. By creating record types with concise and immutable data structures, developers can leverage record patterns to match values against record types and bind variables to corresponding components. This enables robust, declarative, and composable data navigation and processing. Record patterns enhance code readability and maintainability by providing a more concise syntax for deconstructing record values, allowing for streamlined operations such as filtering lists, creating new records from existing ones, and mapping records to other types. Overall, record patterns are a powerful tool that helps developers write more concise, readable, and maintainable code, making them a valuable addition to the Java language.

Leave your thought here

Your email address will not be published. Required fields are marked *