【问题标题】:Detect which field != null in POJO class?在 POJO 类中检测哪个字段!= null?
【发布时间】:2020-02-19 09:17:10
【问题描述】:

我在这里尝试从 Firebase 数据库中获取值。

public class User {

    String id = null;
    String name = null;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

其中一些 getter 字段为空,并且一一检查是冗长的编码。

activityRef.child(uid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    { 
        User usr =dataSnapshot.getValue(User.class); 
        //here i want to check which value!=null 
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

【问题讨论】:

  • 如果值为空,你想做什么?
  • 我想处理 !=null 项目
  • 检查的具体结果是什么? !=null 的字段列表?
  • !=null 的字段列表? ...是的,
  • list不是必须的,datasnapshot只有一个不为空的值

标签: java android


【解决方案1】:

您可以使用反射解决方案,但它会比if (object.getField() != null) {...} 慢得多

我不明白,只有一个值是非空的?

你可以这样实现:

用户类别:

class User {
    private String id;
    private String name;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    // this uses reflection to get the first non-null field
    // you can modify it to return a list of non-null fields
    // in the case I've misunderstood the point
    public Field getFirstNonNullField() {
      for (Field f : getClass().getDeclaredFields()) {
        f.setAccessible(true);
        try {
          if (f.get(this) != null)
            return f;
        } catch (IllegalArgumentException | IllegalAccessException e) {
          // TODO handle this exception as you want
          e.printStackTrace();
        }
      }
      return null;
    }
}

主方法 // 使用示例

public static void main(String[] str) {
    User u = new User();
    u.setName("hello");
    Field f = u.getFirstNonNullField();
    try {
      System.out.print(f.get(u));
    } catch (IllegalArgumentException | IllegalAccessException e) {
      // TODO handle this exception as you want
      e.printStackTrace();
    }
  }

【讨论】:

    【解决方案2】:

    我认为这将帮助您找到解决方案。

    activityRef.child(uid).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                { 
                    User usr =dataSnapshot.getValue(User.class); 
                    for (Field f : User.class.getDeclaredFields()){
                       if(f.get(usr) != null){
    
                        }
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
    

    【讨论】:

    • "some field is null",哪个字段为null,只返回true或false
    • 好的,我明白你的意思了。您想检查哪个字段为空。我误解了这个问题。
    • 我要检查哪个字段不为空
    • 所以我认为您可以获得一个包含非空字段键值对的 Map。它对你有用吗?
    • 怎么做?
    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2016-04-27
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多