如何使用 JAVA 8 检查对象的所有字段是否为 NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48103168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if all the fields of an object is NULL using JAVA 8?
提问by user123475
I want to check if all the fields of an object is null or not using Java 8. Tried different approach as mentioned in herebut I want it to be done using Java 8 feature.
我想使用 Java 8 检查对象的所有字段是否为空。尝试了此处提到的不同方法,但我希望使用 Java 8 功能来完成。
For example:
例如:
class person{
String name;
Long id;
//getter & setter
}
Person person = new Person();
List<Person> personList = new ArrayList<>();
personList.add(person);
I want to add person to list only when id and name is not NULL. Since, Person has more than 10 fields, I dont want to null check each field and add to list. Here, I am setting those fields from ResultSet from DB operation.
我想仅当 id 和 name 不为 NULL 时才将人员添加到列表中。因为 Person 有超过 10 个字段,所以我不想检查每个字段并添加到列表中。在这里,我从 DB 操作的 ResultSet 中设置这些字段。
回答by Woodrow Barlow
Assuming you know all of the fields at compile-time, the best solution is to check each one explicitly. Using reflection for this task would add unnecessary run-time overhead. However, to save yourself a little effort, you can write a function on the class itself to handle this check.
假设您在编译时知道所有字段,最好的解决方案是明确检查每个字段。为此任务使用反射会增加不必要的运行时开销。但是,为了节省一点精力,您可以在类本身上编写一个函数来处理此检查。
class Person {
String name;
Long id;
// constructor, getters, and setters
public boolean isEmpty() {
return (this.name == NULL && this.id == NULL);
}
}
Then your body code might look like this:
那么您的正文代码可能如下所示:
Person person = new Person();
if (!person.isEmpty()) {
personList.add(person);
}
If you don't know all of the fields until run-time, you'll need to use reflection as described by this stackoverflow answer.
如果您直到运行时才知道所有字段,您将需要使用此 stackoverflow answer 中所述的反射。
回答by Mark
If you're willing to use an external package you could use the Lombok @NonNullannotation on either the setter of the method or on the constructor of the class. That will generate checking code that will result in a NullPointerException
at the time that someone attempts to set the value to null
. Essentially, Lombok generates the boiler plate checking code. You just add an annotation (or 10).
如果您愿意使用外部包,您可以在方法的 setter 或类的构造函数上使用 Lombok @NonNull注释。这将生成检查代码,NullPointerException
当有人试图将值设置为 时,该代码将导致null
。本质上,Lombok 生成样板检查代码。您只需添加一个注释(或 10 个)。
Having the exception occur at the time of creating the object is often far preferable to encountering it later (i.e you can catch it and handle it appropriately).
在创建对象时发生异常通常比稍后遇到它要好得多(即您可以捕获它并适当地处理它)。
回答by Calebj
Reflection is neat however it comes at a significant cost.
反射是整洁的,但它需要付出巨大的代价。
The best approach here would be to add an interface for the object forcing the implementation of a null check method. Inside of the implemented null check method you would then add your logic to see if the item properties are null or not.
这里最好的方法是为强制实现空检查方法的对象添加一个接口。在已实现的空检查方法中,您将添加逻辑以查看项目属性是否为空。
If you have your heart set on reflection, which you shouldn't unless you don't know the fields you are receiving, then this question has some answers over here What is the best way to know if all the variables in a Class are null?
如果您一心想反思,除非您不知道您正在接收的字段,否则您不应该这样做,那么这个问题在这里有一些答案什么是知道类中的所有变量是否为空的最佳方法?