javarecord

Java Record custom constructor


I have a java record that takes in a list

public record Zoo(List<Animal> animals ) {

     public Zoo(Collection<Animal> animals) {
         this(new ArrayList<>(animals));
     }

     ...
}

However, the animals are not in sorted order, and I want to create record where animals are sorted. Is this possible in Java record?

In plain java class, I could have

public class Zoo {
   ...
   public Zoo(List<Animal> animals) {
     this.animals = animals.sort(someComparator);
   } 

}

Solution

  • You can do the same as with your "plain Java class".

    import java.util.Collections;
    
    public record Zoo(List<Animal> animals) {
    
      /*
       * The canonical constructor copies the 'animals' list so that both
       * constructors have consistent behavior (i.e. they both result in
       * the list being copied). Plus, since you want to sort the list, it's
       * a good idea to create a copy anyway to avoid side-effects.
       *
       * FIXME: When the non-canonical constructor is used, the 'animals' list
       *        will be copied twice. If anyone can think of a way to avoid
       *        this, please let me know.
       */
    
      // explicit canonical constructor
      public Zoo(List<Animal> animals) {
        animals = new ArrayList<>(animals);
        animals.sort(/* comparator */);
        this.animals = Collections.unmodifiableList(animals);
      }
    
      // a non-canonical constructor; must delegate to canonical constructor
      public Zoo(Collection<Animal> animals) {
        this(new ArrayList<>(animals));
      }
    }
    

    The first constructor is an explicit declaration of the canonical constructor. From the documentation of java.lang.Record:

    A record class has the following mandated members: a canonical constructor, which must provide at least as much access as the record class and whose descriptor is the same as the record descriptor; a private final field corresponding to each component, whose name and type are the same as that of the component; a public accessor method corresponding to each component, whose name and return type are the same as that of the component. If not explicitly declared in the body of the record, implicit implementations for these members are provided.

    [...]

    The primary reasons to provide an explicit declaration for the canonical constructor or accessor methods are to validate constructor arguments, perform defensive copies on mutable components, or normalize groups of components (such as reducing a rational number to lowest terms.)

    Note all other constructors must eventually delegate to the canonical constructor.