Using a Local Record for Readability

Sometimes there is something we really want to put in our cert book that doesn’t fit. You can only have so many case studies/real world scenarios after all. I rescued this from the trash heap and made this blog post./

Suppose we want to count the number of names that have both a first and last name beginning with E. We want to call the method as:

var names = List.of("Sarah Smith", "Eve Edwards");
System.out.println(local.countNamesStartingWithEs(names));

We can read this with streams:

public long countNamesStartingWithEs(List<String> names) {
   return names.stream()
      .map(t -> t.split(" "))
      .filter(a -> a[0].startsWith("E"))
      .filter(a -> a[1].startsWith("E"))
      .count();
}

The code works. However, it has array positions sprinkled throughout. This requires you to keep remembering the format. We can refactor using a local record:

public long countNamesStartingWithEsWithRecords(List<String> names) {
   record Name(String first, String last) {}

   return names.stream()
      .map(t -> t.split(" "))
      .map(t -> new Name(t[0], t[1]))
      .filter(n -> n.first().startsWith("E"))
      .filter(n -> n.last().startsWith("E"))
      .count();
    }

The record lets us give the fields names so we can reference them as n.first() and n.last(). The only place that has to know about the order is the call record constructor. For such a simple example, the approaches are similar. For longer and more complex pipelines, the local record approach can make the code easier to read.