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

Code before:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * BeadSort cannot sort negative numbers, characters, or strings.
 * It only works for non-negative integers.
 *
 * @see <a href="https://en.wikipedia.org/wiki/Bead_sort">BeadSort Algorithm (Wikipedia)</a>
 */
public class BeadSort {
    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }

        int[] sorted = new int[array.length];
        int max = Arrays.stream(array).max().orElse(0);


Review comment (refactor):
The entire class could look something like that:
```java
public class BeadSort {
    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        allInputsMustBeNonnegative(array);
        return extractSortedFromGrid(fillGrid(array));
    }

    private void allInputsMustBeNonnegative(final int[] array) {
        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }
    }

    private char[][] fillGrid(final int[] array) {
        final var maxValue = Arrays.stream(array).max().orElse(0);
        var grid = getEmptyGrid(array.length, maxValue);

        int[] count = new int[maxValue];
        for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
            int k = 0;
            for (int j = 0; j < array[i]; j++) {
                grid[count[maxValue - k - 1]++][k] = '*';
                k++;
            }
        }
        return grid;
    }

    private char[][] getEmptyGrid(final int arrayLength, final int maxValue) {
        char[][] grid = new char[arrayLength][maxValue];
        for (int i = 0; i < arrayLength; i++) {
            for (int j = 0; j < maxValue; j++) {
                grid[i][j] = '-';
            }
        }

        return grid;
    }

    private int[] extractSortedFromGrid(final char[][] grid) {
        int[] sorted = new int[grid.length];
        for (int i = 0; i < grid.length; i++) {
            int k = 0;
            for (int j = 0; j < grid[grid.length - 1 - i].length && grid[grid.length - 1 - i][j] == '*'; j++) {
                k++;
            }
            sorted[i] = k;
        }
        return sorted;
    }
}
```

Please consider the datatype of `grid`: maybe it should be an array of `boolean` or some enum (enum should be more readable).

Code after:
package com.thealgorithms.sorts;

import java.util.Arrays;

public class BeadSort {
    private enum BeadState { BEAD, EMPTY }

    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        allInputsMustBeNonNegative(array);
        return extractSortedFromGrid(fillGrid(array));
    }

    private void allInputsMustBeNonNegative(final int[] array) {
        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }
    }

    private BeadState[][] fillGrid(final int[] array) {
        final var maxValue = Arrays.stream(array).max().orElse(0);
        var grid = getEmptyGrid(array.length, maxValue);

        int[] count = new int[maxValue];