Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test List<String> for empty or nullness?

Tags:

java

It seems no matter what I do I get the wrong result.

My list is defined as follows:

private List<String> selectedPriorities;

Nothing odd or fancy on the getter/setter:

public void setSelectedPriorities(List<String> selectedPriorities) {
    this.selectedPriorities = selectedPriorities;
}

public List<String> getSelectedPriorities() {
    return selectedPriorities;
}

In a session bean I want to alter a different List based on the contents (or lack thereof) of this list.

Here is that code:

List<String> restrictList = new ArrayList<String>();
restrictList.add("lower(logs.clazz) like lower(concat(#{logs.clazz},'%'))");
restrictList.add("lower(logs.rule) like lower(concat(#{logs.rule},'%'))");
PrioritySelectorBean selectorBean = (PrioritySelectorBean) Component.getInstance("prioritySelectorBean",true);
System.out.println("constructRestrictionList selectorBean "+selectorBean.getSelectedPriorities());

if (selectorBean.getSelectedPriorities() == null) {
    System.out.println("IS NULL");
    return restrictList;
}

if (selectorBean.getSelectedPriorities().isEmpty()){
    System.out.println("IS EMPTY");
}

if (selectorBean.getSelectedPriorities().size()<1){
    System.out.println("HAS NOTHING IN IT");
    return restrictList;
}
System.out.println("NOT NULL");
restrictList.add("lower(logs.priority) in (#{prioritySelectorBean.selectedPriorities})");

It always falls through to NOT NULL and adds the string to restrictList. It's making me crazy! How do I detect nothingness in this list? Here is the log snippet

14:24:10,057 INFO  [STDOUT] constructRestrictionList selectorBean []
14:24:10,057 INFO  [STDOUT] NOT NULL
like image 650
april26 Avatar asked Mar 08 '11 19:03

april26


People also ask

How do you check if a list is null or empty?

isEmpty() method of CollectionUtils can be used to check if a list is empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.

How do I check if an ArrayList contains empty string?

ArrayList. isEmpty() method returns true if the ArrayList is empty, and false if it is not empty. In the following example, we will create an ArrayList, add no elements to it, and check if it is empty or not by using ArrayList. isEmpty() method.

How do I check if a list is empty or null in Python?

In this solution, we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.


1 Answers

You can get the result that you're seeing if the list contains a single zero-length string:

List<String> list = new ArrayList<String>();
list.add("");

System.out.println("blah = " + list);  // displays "blah = []"
if (list.isEmpty()) {
    System.out.println("Empty"); // doesn't get displayed
}
like image 126
Oliver Charlesworth Avatar answered Sep 20 '22 20:09

Oliver Charlesworth