有没有一种方法可以检查对象的属性是否具有值?

时间:2020-04-28 08:39:00

标签: java loops class

我有一个对象,里面装有数据库中的数据。现在,我想知道还是必须知道该对象的哪些属性中是否包含值。

我的实体类别:

public class PurchasingDeadlineValuesEntity {
@Expose
private Long purchasingDeadlineValuesId;
@Expose private Long purchasingDeadlineId;
@Expose private Long mandantKagId;
@Expose private double zero;
@Expose private double one;
@Expose private double two;
@Expose private double three;
@Expose private double four;
@Expose private double five;
@Expose private double six;
@Expose private double seven;
@Expose private double eight;
@Expose private double nine;
@Expose private double ten;
@Expose private double eleven;
@Expose private double twelve;
@Expose private double thirteen;
private Timestamp dateCreated;
private Timestamp dateUpdated;

//getter & Setter..and so 

我考虑过从类中获取声明的字段,并将它们与对象的属性进行比较。但是,我该如何进一步。

这是我尝试失败的尝试:

    protected HashMap<Integer, HashMap<String, Double>> extractKagAndPurchasingDeadlines() {
    Field[] fields = PurchasingDeadlineValuesEntity.class.getDeclaredFields();

    for (MandantKagAccountEntity mandantKagAccountEntity : m_k_a_E) {
        PurchasingDeadlineValuesEntity purchasingDeadlineValuesEntity = mandantKagAccountEntity.getPurchasingDeadlineValuesEntity();
        for (Field field : fields) {
            if (field.getType().equals(double.class)) {

            }
        }
    }
}

所以我现在需要,如果PurchasingDeadline对象中double字段的值大于0.0。但是它们也可以为空。

有人可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:1)

我想,这就是你想要的:

for (Field field : fields) {
    if (field.getType().equals(double.class)) {
        final double value = (double) field.get(purchasingDeadlineValuesEntity);
        if (value > 0.0) {
            // do whatever you want with `value`
        }
    }
}

这是完整的工作示例:

import java.lang.reflect.Field;

class Scratch {
    public static void main(String[] args) throws IllegalAccessException {

        final PurchasingDeadlineValuesEntity purchasingDeadlineValuesEntity = new PurchasingDeadlineValuesEntity();
        purchasingDeadlineValuesEntity.setDeadlineId(2L);
        purchasingDeadlineValuesEntity.setZero(0.0);
        purchasingDeadlineValuesEntity.setOne(2.0);

        Field[] fields = PurchasingDeadlineValuesEntity.class.getDeclaredFields();

        for (Field field : fields) {
            if (field.getType().equals(double.class)) {
                final double value = (double) field.get(purchasingDeadlineValuesEntity);
                if (value > 0.0) {
                    System.out.println("field name = " + field.getName());
                    System.out.println("field value = " + value);
                }
            }
        }
    }

    public static class PurchasingDeadlineValuesEntity {
        private Long deadlineId;

        private double zero;

        private double one;

        private double two;

        public Long getDeadlineId() {
            return deadlineId;
        }

        public void setDeadlineId(Long deadlineId) {
            this.deadlineId = deadlineId;
        }

        public double getZero() {
            return zero;
        }

        public void setZero(double zero) {
            this.zero = zero;
        }

        public double getOne() {
            return one;
        }

        public void setOne(double one) {
            this.one = one;
        }

        public double getTwo() {
            return two;
        }

        public void setTwo(double two) {
            this.two = two;
        }
    }
}