Java: Generic minimum and maximum

Write a bounded generic method once and use it for numbers, words and your own comparable type.

What to build

A program that finds the minimum and maximum of three kinds of list with one generic method.

Requirements

  • Write a generic Pair<A, B> and static <T extends Comparable<T>> Pair<T, T> minMax(List<T> items).
  • Write a record Version(int major, int minor) that implements Comparable<Version> (by major, then minor) and prints as MAJOR.MINOR.
  • Read a line of whole numbers, a line of words and a line of versions such as 1.10 1.9 2.0, and print numbers: MIN MAX, words: MIN MAX (alphabetical by compareTo) and versions: MIN MAX.

Input

Three lines, each with at least one item, separated by spaces.

Output

Three lines.

Constraints

At most 100 items per line; versions are MAJOR.MINOR with whole numbers.

Example

Input:

5 -2 9
pear apple fig
1.10 1.9 2.0

Output:

numbers: -2 9
words: apple pear
versions: 1.9 2.0

Why it works

The bound lets minMax call compareTo, so the same method works for Integer, String and Version, and comparing versions by numbers puts 1.10 after 1.9, which comparing them as text would not.

Examples

Example 1 · Input
5 -2 9
pear apple fig
1.10 1.9 2.0
Expected output
numbers: -2 9
words: apple pear
versions: 1.9 2.0
Example 2 · Input
7
only
3.3
Expected output
numbers: 7 7
words: only only
versions: 3.3 3.3

Hints

Hint 1

static <T extends Comparable<T>> Pair<T, T> minMax(List<T> items) returns a Pair of the smallest and largest.

Hint 2

Write a generic Pair<A, B> record or class to return two values.

Solution

Show one correct solution (try it yourself first)
import java.util.*;

public class Main {
    record Pair<A, B>(A first, B second) {}

    record Version(int major, int minor) implements Comparable<Version> {
        public int compareTo(Version o) {
            return major != o.major ? Integer.compare(major, o.major) : Integer.compare(minor, o.minor);
        }

        @Override
        public String toString() {
            return major + "." + minor;
        }
    }

    static <T extends Comparable<T>> Pair<T, T> minMax(List<T> items) {
        T min = items.get(0);
        T max = items.get(0);
        for (T item : items) {
            if (item.compareTo(min) < 0) min = item;
            if (item.compareTo(max) > 0) max = item;
        }
        return new Pair<>(min, max);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Integer> nums = new ArrayList<>();
        for (String s : in.nextLine().trim().split(" ")) nums.add(Integer.parseInt(s));
        List<String> words = new ArrayList<>(Arrays.asList(in.nextLine().trim().split(" ")));
        List<Version> versions = new ArrayList<>();
        for (String s : in.nextLine().trim().split(" ")) {
            String[] p = s.split("\\.");
            versions.add(new Version(Integer.parseInt(p[0]), Integer.parseInt(p[1])));
        }
        Pair<Integer, Integer> a = minMax(nums);
        Pair<String, String> b = minMax(words);
        Pair<Version, Version> c = minMax(versions);
        System.out.println("numbers: " + a.first() + " " + a.second());
        System.out.println("words: " + b.first() + " " + b.second());
        System.out.println("versions: " + c.first() + " " + c.second());
    }
}

Lesson: Generics →