Fri. Apr 19th, 2024
empty container?

Again providing two very simple methods here for my ListUtils class. Nothing fancy. Most ListUtils you find will have an isEmpty(). I’ve added the notEmtpy() for readability. Yes I could do this:

if(!ListUtils.isEmpty(myList)) {
   ....
}

However I find this far more readable:

if(ListUtils.notEmpty(myList)) {
   ...
}

So here for the sake of showing what all I have added are the two methods:

/**
     * Returns boolean indicating if the List is empty
     * @param list List to check
     * @param <E> List type
     * @return boolean indicating if LIst is empty
     */
    public static <E> boolean isEmpty(List<E> list) {
        if (list != null) {
            if (list.size() > 0) {
                return false;
            }
        }

        return true;
    }

    /**
     * Returns boolean indicating if the List is not empty
     * @param list List to check
     * @param <E> List type
     * @return boolean indicating if List is not empty
     */
    public static <E> boolean notEmpty(List<E> list) {
        return !ListUtils.isEmpty(list);
    }

Nothing fancy and just straight and simple code. For the full class look at this GIST.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.