基本上,你如何检查一个对象是否为null或空。我的意思是,如果我实例化了一个对象,但它的所有值或字段都为null,那么我该如何在代码中检查它是否为空?
我已经尝试过以下方法;
if (doc != null){
.... do something
但它似乎不起作用。
基本上,你如何检查一个对象是否为null或空。我的意思是,如果我实例化了一个对象,但它的所有值或字段都为null,那么我该如何在代码中检查它是否为空?
我已经尝试过以下方法;
if (doc != null){
.... do something
但它似乎不起作用。
您不能直接这样做,您应该提供自己的检查方式。例如:
class MyClass {
Object attr1, attr2, attr3;
public boolean isValid() {
return attr1 != null && attr2 != null && attr3 != null;
}
}
或者将所有字段都声明为final,并在构造函数中初始化,以确保所有字段都被正确地初始化。
null
的字段的需求。这始终是关于空值的相同问题。 - Jackimport org.apache.commons.lang3.ObjectUtils;
if(ObjectUtils.isEmpty(yourObject)){
//your block here
}
可以使用Java反射来实现此操作。如果对象存在任何一个属性值,则该方法返回false。希望能对某些人有所帮助。
public boolean isEmpty() {
for (Field field : this.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
if (field.get(this)!=null) {
return false;
}
} catch (Exception e) {
System.out.println("Exception occured in processing");
}
}
return true;
}
你应该将其与 null
进行比较。
如果你想检查对象 x 是否为 null,可以这样做:
if(x != null)
但如果它不为空,它可以具有空值或空字符串的属性。您需要显式检查这些内容:
if(x.getProperty() != null)
"empty"检查取决于所涉及的类型。对于Java "String",通常可以执行以下操作:
if(str != null && !str.isEmpty())
我建议您添加独立的重载方法并将它们添加到您项目的Utility/Utilities类中。
检查集合是否为空或为null
public static boolean isEmpty(Collection obj) {
return obj == null || obj.isEmpty();
}
或者使用Apache Commons CollectionUtils.isEmpty()
检查Map是否为空或null
public static boolean isEmpty(Map<?, ?> value) {
return value == null || value.isEmpty();
}
或者使用Apache Commons MapUtils.isEmpty()
检查字符串是否为空或null
public static boolean isEmpty(String string) {
return string == null || string.trim().isEmpty();
}
或者使用Apache Commons StringUtils.isBlank()
。
检查对象是否为null很容易,但验证其是否为空很棘手,因为对象可能具有许多私有或继承变量和嵌套对象,这些都应该为空。为此,需要验证所有内容或在所有对象中添加一些isEmpty()方法来验证对象是否为空。
对于简单的情况(在我的项目中已经起作用)。 如果某些字段不需要进行空值检查,则从toString()方法中排除它,就像我上面的代码中删除了school一样。
public class Student {
private String name;
private String school;
private Integer roll;
private String section;
//getters and setters
@Override
public String toString() {
return "Student [name=" + name + ", roll=" + roll + ", section=" + section + "]";
}
}
public class StudentRunner {
public static void main(String[] args) {
Student s = new Student();
s.setName("ved");
s.setRoll(12);
s.setSection("A");
s.setSchool(null);//school set null and it removed from toString() method
if(s.toString().contains("null")) {
System.out.println("null value contains");
//do your work here or throw exception
} else {
System.out.println("no null value");
}
}
}
Person Object
name Property with getter and setter
to check if name is not initialized.
Person p = new Person();
if(p.getName()!=null)
我有一个方法,你们告诉我它好不好。
创建一个类的新对象,将其与您要检查是否为空的对象进行比较。
为了正确执行:
重写您的模型类的hashCode()和equals()方法,以及您的类成员所属的类的对象的这些方法,例如:
人类(主要模型类):
public class Person {
private int age;
private String firstName;
private String lastName;
private Address address;
//getters and setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
@Override
public String toString() {
return "Person [age=" + age + ", firstName=" + firstName + ", lastName=" + lastName + ", address=" + address
+ "]";
}
}
地址类(在人类内部使用):
public class Address {
private String line1;
private String line2;
//getters and setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((line1 == null) ? 0 : line1.hashCode());
result = prime * result + ((line2 == null) ? 0 : line2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (line1 == null) {
if (other.line1 != null)
return false;
} else if (!line1.equals(other.line1))
return false;
if (line2 == null) {
if (other.line2 != null)
return false;
} else if (!line2.equals(other.line2))
return false;
return true;
}
@Override
public String toString() {
return "Address [line1=" + line1 + ", line2=" + line2 + "]";
}
}
现在在主类中:
Person person1 = new Person();
person1.setAge(20);
Person person2 = new Person();
Person person3 = new Person();
if(person1.equals(person2)) --> this will be false
if(person2.equals(person3)) --> this will be true
我希望这是最好的方法,而不是在每个成员变量上放置if条件。
告诉我吧!
假设,
data = {};
if( if(!$.isEmptyObject(data)))
{
document.write("Object is empty);
}
else{
document.write("Object is not empty);
}
对我来说很有效,并且这是一种简单的方法来检查对象是否为空。
isEmpty()
的方法,这并不意味着它不使用任何内存,而且在满时可能使用相同量的内存,例如 ArrayBlockingQueue。它是一种表明对象不包含任何其他可获取对象的方法。这是只有组件的开发人员才能确定的内容。 - Peter Lawrey