似乎无论我做什么,我都会得到错误的结果。

我的列表定义如下:

private List<String> selectedPriorities;

getter/setter 没有什么奇怪的或花哨的:
public void setSelectedPriorities(List<String> selectedPriorities) {
    this.selectedPriorities = selectedPriorities;
}

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

在 session bean 中,我想根据此列表的内容(或缺少内容)更改不同的列表。

这是代码:
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})");

它总是落入 NOT NULL 并将字符串添加到restrictList。它让我发疯!如何检测此列表中的虚无?这是日志片段
14:24:10,057 INFO  [STDOUT] constructRestrictionList selectorBean []
14:24:10,057 INFO  [STDOUT] NOT NULL

最佳答案

如果列表包含单个零长度字符串,您可以获得您看到的结果:

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
}

关于java - 如何测试 List<String> 是否为空或为空?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5237261/

10-16 04:54