Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/PigeonholeSort.java (Java)

Code before:
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length < 2) {
            return array;
        }

        checkForNegativeInput(array);

        final int maxElement = Arrays.stream(array).max().orElseThrow();
        final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);

        populatePigeonHoles(array, pigeonHoles);
        collectFromPigeonHoles(array, pigeonHoles);

        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");
            }
        }
    }

    /**
     * Creates pigeonholes for sorting using an ArrayList of ArrayLists.
     *
     * @param maxElement the maximum element in the array
     * @return an ArrayList of ArrayLists
     */
    private static List<List<Integer>> createPigeonHoles(int maxElement) {
        List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1);
        for (int i = 0; i <= maxElement; i++) {
            pigeonHoles.add(new ArrayList<>());
        }
        return pigeonHoles;
    }

    /**
     * Populates the pigeonholes with elements from the array.
     *
     * @param array the array to be sorted
     * @param pigeonHoles the pigeonholes to be populated

Review comment (suggestion):
```suggestion
        for (final int number : array) {
```

Code after:
     * @return the sorted array
     */
    public static int[] sort(int[] array) {

        checkForNegativeInput(array);

        if (array.length == 0) {
            return array;
        }

        final int maxElement = Arrays.stream(array).max().orElseThrow();
        final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);

        populatePigeonHoles(array, pigeonHoles);
        collectFromPigeonHoles(array, pigeonHoles);

        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (final int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains negative integers.");
            }
        }
    }

    /**
     * Creates pigeonholes for sorting using an ArrayList of ArrayLists.
     *
     * @param maxElement the maximum element in the array
     * @return an ArrayList of ArrayLists
     */
    private static List<List<Integer>> createPigeonHoles(int maxElement) {
        List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1);
        for (int i = 0; i <= maxElement; i++) {
            pigeonHoles.add(new ArrayList<>());
        }
        return pigeonHoles;
    }

    /**
     * Populates the pigeonholes with elements from the array.
     *
     * @param array the array to be sorted