Start learning free

LESSON 262 OF 671 · RECORDS, SORTING & BUILT-INS

Oldest Pet in Java: Records and a Strict Comparison

Practice Read the full explanation

You get an ArrayList of pets, pets. Each pet is a pair: its name and its age, like ("Rex", 4).

Return the name of the oldest pet.

Several pets may share the oldest age. Then return the one that comes first in pets.

The starter code shows how your language holds a pair.

 

Example 1:

Input: pets = [("Rex", 4), ("Bella", 7), ("Max", 2)]
Output: "Bella"
Explanation: Bella is 7.
Rex and Max are younger.

Example 2:

Input: pets = [("Luna", 3), ("Coco", 9), ("Milo", 9)]
Output: "Coco"
Explanation: Coco and Milo are both 9.
Coco comes first in the list.

Example 3:

Input: pets = [("Kiki", 0)]
Output: "Kiki"
Explanation: Kiki is the only pet.

 

Constraints:

  • pets has at least one pet.
  • Every age is a whole number from 0 to 30.
  • Each name is a capital letter followed by lowercase letters, like "Rex".
STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · RECORDS, SORTING & BUILT-INS

    Oldest Pet in Java, explained

    Problem overview

    The method oldestPet gets a list of Pet records, each with a name and an age, and returns the name of the oldest one. If several pets share the top age, the one that comes first in the list wins. The lesson provides the record: record Pet(String name, int age) {}.

    For Rex 4, Bella 7, Max 2 the answer is Bella. For Luna 3, Coco 9, Milo 9 it is Coco, because Coco and Milo are both 9 and Coco comes first.

    Approach

    Keep the oldest pet found so far and compare each pet with it. Store the whole Pet, not just the age, so its name comes along. One pass is enough.

    Step by step

    1. Start oldest as the first pet, pets.get(0). Before the loop looks at anything else, it is the oldest you have seen.
    2. Loop over the list with for (Pet pet : pets).
    3. Inside, test pet.age() > oldest.age(). The comparison is strict. A pet of the same age must not replace the one you have, which keeps the earlier one on a tie.
    4. When the test passes, set oldest = pet.
    5. After the loop, return oldest.name().

    Following the tie

    Take Luna 3, Coco 9, Milo 9. This shows the name held by oldest after the loop looks at each pet:

    petoldest after the test
    LunaLuna
    CocoCoco
    MiloCoco

    Coco is older than Luna, so she takes over. Milo is exactly as old as Coco, which is not older, so Coco stays. The strict test is what turns "first in the list" into code, with no extra rule.

    Pets are records, so oldest = pet copies a reference to a record that never changes. Nothing can alter the pet you are holding by accident.

    The solution in Java

    class Solution {
        public String oldestPet(List<Pet> pets) {
            Pet oldest = pets.get(0);
            for (Pet pet : pets) {
                if (pet.age() > oldest.age()) {
                    oldest = pet;
                }
            }
            return oldest.name();
        }
    }
    

    Reading a record's values takes parentheses: pet.age() and oldest.name(). The loop starts with the first pet again, compares it with itself and changes nothing, which is harmless.

    Edge cases and common mistakes

    • Using >=. On Luna 3, Coco 9, Milo 9 it returns Milo, because the later pet replaces the earlier one on a tie. The expected answer is Coco.
    • Keeping only the age. A variable holding just the oldest age can't give you the name at the end. Keep the whole pet.
    • One pet. Kiki 0 returns Kiki. Starting from the first pet, instead of from a made-up age of 0, makes this case work without special handling.

    Testing

    Check your method on these lists. The expected results come from the lesson's examples:

    • Rex 4, Bella 7, Max 2 returns Bella.
    • Luna 3, Coco 9, Milo 9 returns Coco.
    • Kiki 0 returns Kiki.

    Also try a list where the oldest pet is first, and one where it is last. A loop that works when the answer is in the middle but not at an end usually has a wrong starting value or a wrong loop range. Here neither can go wrong, because the loop covers every pet and starts from a real one.

    The method visits each pet once and keeps a single extra variable, so its time grows in step with the number of pets and it needs no extra memory beyond that variable. Sorting the pets by age would also find the oldest, but it does more work and has to deal with ties explicitly.

    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free