Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
2
Indexable
Never
import java.util.*;

/**
 * A utility class for working with lists.
 *
 * Lists, lists, we love them so
 * They help us organize and grow
 * But sometimes we need some help
 * To manage them and make them yelp
 *
 * This class is here to lend a hand
 * With methods that you'll understand
 * Shuffle, find, and get with ease
 * With ListUtils, your lists will please
 */
public class ListUtils {

    /**
     * Shuffle, shuffle, your list with ease
     * Using Collections is the way to please
     * Call the method, and you will see
     * Your list will be shuffled randomly
     *
     * Shuffle the elements of the specified list randomly using the
     * {@link java.util.Collections#shuffle(List)} method.
     *
     * @param list the list to shuffle
     * @param <T>  the type of elements in the list
     */
    public static <T> void shuffleList(List<T> list) {
        Collections.shuffle(list);
    }

    /**
     * Find the minimum and maximum values in your collection
     * With this method, you'll have no objection
     * Just pass in your comparator, and it will be
     * Sorted and found with simplicity
     *
     * Find the minimum and maximum values in the specified collection using
     * the specified comparator to compare elements.
     *
     * @param collection  the collection to search
     * @param comparator  the comparator to use for comparing elements
     * @param <T>         the type of elements in the collection
     * @return an Optional containing a Pair of the minimum and maximum elements,
     *         or an empty Optional if the collection is null or empty
     */
    public static <T> Optional<Pair<T, T>> findMinMax(Collection<T> collection, Comparator<T> comparator) {
        if (collection == null || collection.isEmpty()) {
            return Optional.empty();
        }
        T min = Collections.min(collection, comparator);
        T max = Collections.max(collection, comparator);
        return Optional.of(new Pair<>(min, max));
    }

    /**
     * Random, random, pick one out
     * From your list, with just one shout
     * Use this method, it's quick and neat
     * And randomness will be complete
     *
     * Get a random element from the specified list using the
     * {@link java.util.Collections#shuffle(List)} method and returning the
     * first element of the shuffled list.
     *
     * @param list the list to select an element from
     * @param <T>  the type of elements in the list
     * @return a random element from the list, or null if the list is empty
     */
    public static <T> T getRandomElement(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        Collections.shuffle(list);
        return list.get(0);
    }
}

In this updated javadoc, we use a song to introduce the class and describe its purpose. We then provide song lyrics for each of the methods in the class, describing what they do and how to use them. We also include standard javadoc documentation for each method, including parameter and return value descriptions.