简体   繁体   English

如何比较两个复杂的对象并忽略空字符串和空字符串?

[英]How to compare two complex object and ignore both null and empty string?

I need to assert two complex objects which may contains both primitive and n levels of objects within. 我需要声明两个复杂的对象,其中可能包含原始对象和n个对象。 so i googled and chose a library unitils . 所以我用谷歌搜索并选择了一个unitils图书馆。 But i have condition that i need to consider both the null and empty string as equal.But here it only supports ReflectionComparatorMode.IGNORE_DEFAULTS . 但是我有一个条件,我需要将null和空字符串都视为相等。但是这里它仅支持ReflectionComparatorMode.IGNORE_DEFAULTS Scenario: 场景:

 public class AssertVerify {

    public static void main(String args[]){

    CustomerPojo cuPojo1=new CustomerPojo();
    CustomerPojo cuPojo2=new CustomerPojo();

    cuPojo1.setCustomerName("obuli");
    cuPojo1.setCustomerAge("20");
    cuPojo1.setAddress("");

    cuPojo2.setCustomerName("obuli");
    cuPojo2.setCustomerAge("20");


    /**
     * Asserting  two pojos
     */     
    ReflectionAssert.assertReflectionEquals(cuPojo1, cuPojo2,
            ReflectionComparatorMode.LENIENT_DATES ,ReflectionComparatorMode.IGNORE_DEFAULTS);

}       

Error: 错误:

 junit.framework.AssertionFailedError: 
 Expected: CustomerPojo<customerName="obuli", customerAge="20", Address="">
  Actual: CustomerPojo<customerName="obuli", customerAge="20", Address=null>

  --- Found following differences ---
  Address: expected: "", actual: null

   --- Difference detail tree ---
  expected: CustomerPojo<customerName="obuli", customerAge="20", Address="">
   actual: CustomerPojo<customerName="obuli", customerAge="20",   Address=null>

  Address expected: ""
  Address   actual: null


   at junit.framework.Assert.fail(Assert.java:47)
    at      org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals(ReflectionA   ssert.java:136)at       org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals(ReflectionA   ssert.java:99)
atcom.assertion.verify.AssertVerify.main(AssertVerify.java:52) 

 picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true

I need to add an rule that consider both null and empty string as equal. 我需要添加一个将空字符串和空字符串视为相等的规则。

Is there is any possible solution available for my situation. 有没有可能针对我的情况的解决方案。

As was mentioned by Gremash you shoud implement comparable interface and override equals method , dont rush this , make sure you know what you doing since you can mess up work in collections when you use your own objects that implement this interface and use your method.Take a look(if possible) at Core Java 1-2 series this problematic is discussed in these books and detaily explained. 正如Gremash提到的那样,您应该实现可比的接口并覆盖equals方法,不要着急,确保您知道自己在做什么,因为当您使用实现此接口的自己的对象并使用您的方法时,可能会使集合中的工作混乱。在Core Java 1-2系列中(如果可能的话)对此问题进行了讨论,并对其进行了详细说明。 There are some rules for instance equals() must define an equivalence relation (it must be reflexive, symmetric, and transitive), more on that : What issues should be considered when overriding equals and hashCode in Java? 有一些规则,例如equals()必须定义一个等价关系(它必须是自反的,对称的和可传递的),此外: 在Java中重写equals和hashCode时应考虑哪些问题?

I think its still better to go this way then rely on other library. 我认为最好还是采用这种方式,然后再依赖其他库。

暂无
暂无

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

相关问题 比较字符串和空对象 - Compare String with Null Object 如何告诉 Jackson 在序列化过程中忽略带有 EMPTY 或 NULL 字段的 Object? - How to tell Jackson to ignore a Object with EMPTY or NULL fields during serialization? 反序列化期间将空字符串忽略为null - Ignore empty string as null during deserialization 从json字符串反序列化到对象时如何忽略null? - how to ignore null when deserializing from json string to object? 比较可以为空和空字符串(“”)的对象 - Compare objects which can be null and empty string (“”) 我有两个 hashmap HashMap <string,list<string> &gt; 在这种格式下,如何比较两个值是否相同或不同的键</string,list<string> - I have two hashmap HashMap<String,List<String>> in this format how to compare both the values are same or not with same keys 如何通过值的数据类型比较两个 JSON 字符串,它应该忽略确切的值,它还应该忽略数组的长度,如下例所示 - How to compare two JSON String by data type of values, it should ignore exact values and it should also ignore length of array as per below example 如何检查列表 object 有 null 值或空字符串? - How to check list object has null value or empty string? 比较两个字符串数组的内容,并删除两个字符串数组中都出现的字符串 - Compare contents of two string arrays, and remove strings which occur in both 使用 Java 比较两个 CSV 文件,条件是忽略两个文件中的特定列 - Compare two CSV files using Java with the condition to ignore a particular column in both
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM