Record Class Person

java.lang.Object
java.lang.Record
cs1302.oracle.model.Person
All Implemented Interfaces:
Serializable, Comparable<Person>

public record Person(int age, String name) extends Record implements Comparable<Person>, Serializable
Represents a "person" record with an age and name.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Sample data related to people.
    static interface 
    Represents a function that accepts one Person argument and produces a result.
    static class 
    Implementations of Comparable<Person> that impose various orderings.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Person(int age, String name)
    Creates an instance of a Person record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    age()
    Returns the value of the age record component.
    int
    Compares this person with the other person by age (ascending), then by name (ascending).
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the name record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Person

      public Person(int age, String name)
      Creates an instance of a Person record class.
      Parameters:
      age - the value for the age record component
      name - the value for the name record component
  • Method Details

    • compareTo

      public int compareTo(Person other)
      Compares this person with the other person by age (ascending), then by name (ascending). If the Person objects being compared have different ages, then they are compared according to the natural ordering of their ages; otherwise, they are compared according to the lexicographic ordering of their names.

      Natural Ordering

      This method defines the natural ordering for Person objects. Some other orderings are provided in the Person.Order class.
      Natural Ordering for Person Objects
      int ret = obj.compareTo(other)
      Return Value Order
      ret < 0 obj is less than other
      ret == 0 obj is equal to other
      ret > 0 obj is greater than other

      Examples

      The following code snippet creates four new Person objects:
      Person bob = new Person(24, "Bob");
      Person kim = new Person(25, "Kim");
      Person joe = new Person(22, "Joe");
      Person ash = new Person(22, "Ash");
      The natural ordering for those objects is:
      1. Person[age=22, name=Ash]
      2. Person[age=22, name=Joe]
      3. Person[age=24, name=Bob]
      4. Person[age=25, name=Kim]
      Specified by:
      compareTo in interface Comparable<Person>
      Parameters:
      other - the other person to compare to
      Returns:
      a negative integer, zero, or positive integer if this person is less than, equal to, or greater than the other person with respect to their natural ordering
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • age

      public int age()
      Returns the value of the age record component.
      Returns:
      the value of the age record component
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component