简体   繁体   English

如何使用 JAVA 8 检查对象的所有字段是否为 NULL?

[英]How to check if all the fields of an object is NULL using JAVA 8?

I want to check if all the fields of an object is null or not using Java 8. Tried different approach as mentioned in here but 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.我想仅当 id 和 name 不为 NULL 时才将人员添加到列表中。 Since, Person has more than 10 fields, I dont want to null check each field and add to list.因为 Person 有超过 10 个字段,所以我不想检查每个字段并添加到列表中。 Here, I am setting those fields from ResultSet from DB operation.在这里,我从 DB 操作的 ResultSet 中设置这些字段。

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 中所述的反射。

If you're willing to use an external package you could use the Lombok @NonNull annotation on either the setter of the method or on the constructor of the class.如果您愿意使用外部包,您可以在方法的 setter 或类的构造函数上使用 Lombok @NonNull注释。 That will generate checking code that will result in a NullPointerException at the time that someone attempts to set the value to null .这将生成检查代码,当有人试图将值设置为null时,该代码将导致NullPointerException Essentially, Lombok generates the boiler plate checking code.本质上,Lombok 生成样板检查代码。 You just add an annotation (or 10).您只需添加一个注释(或 10 个)。

Having the exception occur at the time of creating the object is often far preferable to encountering it later (ie you can catch it and handle it appropriately).在创建对象时发生异常通常比稍后遇到它要好得多(即您可以捕获它并适当地处理它)。

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?如果你一心想反思,除非你不知道你正在接收的字段,否则你不应该这样做,那么这个问题在这里有一些答案什么是知道类中的所有变量是否为空的最佳方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 8:如何检查对象的所有字段是否为 NULL,除了一个 - Java 8: How can I check if all the fields of an object are NULL except from one 当使用springboot时,如何创建JsonDeserializer来将所有JSON中等于“”的Java对象字段设置为null? - How to create JsonDeserializer that sets null to all Java object fields that equals to “” in JSON when using springboot? 如何在Java中检查object是否为null? - How to check if object is null in Java? Java:如何检查对象是否为空? - Java: How to check if object is null? 如何使用Optional在java中检查集合中模型的所有属性的非空? - How to check Not Null for all the attributes of a Model in a Collection in java using Optional? Java对象空检查 - Java null check on object 如何检查Java中的属性(对象)是否为空 - How check if an Attribute(object) is null in Java 如何在 JAVA 中检查 Object 的变量是否为空或 NULL 的变量? - How to check the variables of an Object are Empty or NULL in JAVA? 如何减少java中object的所有字段 - How to reduce all the fields of object in java 如何在将记录插入数据库表Java中之前检查非空字段 - How to check for not null fields before inserting record into a database table Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM