Java返回一个空列表 [英] Java return an empty list
本文介绍了Java返回一个空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Java方法如下;
I have my Java method as below;
public List<Lookup> findAll(String lang) {
Query query = entityManager.createNamedQuery("Lookup.findAll");
if (isValidLang(lang)) {
query.setParameter("lang", lang);
return query.getResultList();
} else {
//return empty list
}
}
现在该方法为lang的有效匹配返回List。
Now the method returns List for valid matches of lang.
但如果不是这样,我想返回一个空列表。我的问题是如何更新代码和返回空列表的最佳方法是什么,以便代码不会失败?
But if that is not the case, I want to return an empty list. My question is how do I update the code & what is the best way to return an empty list so that the code does not fail ?
推荐答案
这应该这样做:
} else {
return Collections.emptyList();
}
这篇关于Java返回一个空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文