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
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With