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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Sample data related to people.static interface
Represents a function that accepts onePerson
argument and produces a result.static class
Implementations ofComparable<Person>
that impose various orderings. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
age()
Returns the value of theage
record component.int
final boolean
Indicates whether some other object is "equal to" this one.final int
hashCode()
Returns a hash code value for this object.name()
Returns the value of thename
record component.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
Person
Creates an instance of aPerson
record class.- Parameters:
age
- the value for theage
record componentname
- the value for thename
record component
-
-
Method Details
-
compareTo
Compares this person with the other person by age (ascending), then by name (ascending). If thePerson
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 forPerson
objects. Some other orderings are provided in thePerson.Order
class.Natural Ordering for Person
Objectsint 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 newPerson
objects:
The natural ordering for those objects is:Person bob = new Person(24, "Bob"); Person kim = new Person(25, "Kim"); Person joe = new Person(22, "Joe"); Person ash = new Person(22, "Ash");
Person[age=22, name=Ash]
Person[age=22, name=Joe]
Person[age=24, name=Bob]
Person[age=25, name=Kim]
- Specified by:
compareTo
in interfaceComparable<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
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. -
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. -
equals
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 withObjects::equals(Object,Object)
; primitive components are compared with '=='. -
age
public int age()Returns the value of theage
record component.- Returns:
- the value of the
age
record component
-
name
Returns the value of thename
record component.- Returns:
- the value of the
name
record component
-