如何检查 JAVA 中对象的变量是 Empty 还是 NULL?

How to check the variables of an Object are Empty or NULL in JAVA?

如何检查 variables of Objects 是 JAVA 中的 empty or NULL

Employee employee = repository.getEmployee(id).orElseGet(null);
if (employee == null) {
   employee = new Employee();
   employee.id(1);
   employee.name("Thirumal");
   employee.mobile("+91-8973-697-871");
   repository.save(employee);
}

我试图创建一个对象 new Employee()

而不是 NULL
Employee employee = repository.getEmployee(id).orElseGet(new Employee());
if (employee.isEmpty()) { //--------- How to do ????????
   employee = new Employee();
   employee.id(1);
   employee.name("Thirumal");
   employee.mobile("+91-8973-697-871");
   repository.save(employee);
}

如何检查 member of objectsempty/NULL

只需将 isEmpty 方法添加到 Employee class 并在那里实现它,或者使用 null object pattern 而不是 new Employee().

至于您在代码中实际尝试做的事情,我建议改为这样做:

Employee employee = repository.getEmployee(id).orElseGet(() -> {
   Employee emp = new Employee();
   emp.id(1);
   emp.name("Thirumal");
   emp.mobile("+91-8973-697-871");
   return repository.save(emp);
});