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.
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.