Java-如何检查两个ArrayList是否是唯一的对象(即使它们具有相同的值)?
我想知道一个给定的Java-如何检查两个ArrayList是否是唯一的对象(即使它们具有相同的值)?,java,arraylist,collections,Java,Arraylist,Collections,我想知道一个给定的ArrayList是否与另一个ArrayList是不同的对象,即使它们的列表中包含相同的对象。我正在测试包含ArrayList的父对象的一些复制逻辑,我想确保这里的未来开发人员不会在逻辑期间简单地重新分配数组列表 例如,如果我有一个模型类,该类包含一个数组列表属性,该属性包含名为值的整数对象,我想执行以下操作: // Create the original value Model model1 = ... // Copy the original value into a n
ArrayList
是否与另一个ArrayList
是不同的对象,即使它们的列表中包含相同的对象。我正在测试包含ArrayList
的父对象的一些复制逻辑,我想确保这里的未来开发人员不会在逻辑期间简单地重新分配数组列表
例如,如果我有一个模型
类,该类包含一个数组列表
属性,该属性包含名为值
的整数
对象,我想执行以下操作:
// Create the original value
Model model1 = ...
// Copy the original value into a new object
Model model2 = modelCopier(model1);
// Check that they are not equal objects
assertNotEquals(model1, model2);
// Check that their values properties are not equal objects
assertNotEquals(model1.values, model2.values);
// Check that their values properties contain the same values though!
assertEquals(model1.values.size(), model2.values.size());
Integer value1 = model1.values.get(0);
Integer value2 = model2.values.get(0);
assertEquals(value1, value2);
这应该证明我们正在复制模型
对象及其值
属性,但列表中的值是相等的
现在这会失败,因为assertNotEquals(model1.values,model2.values)
失败,这是有意义的,因为List
类重写了equals
方法:
比较指定对象与此列表是否相等。退换商品
当且仅当指定对象也是列表时为true,两个列表
具有相同的大小,且所有对应的两个元素对
名单是平等的。(如果(e1==null,则两个元素e1和e2相等。)?
e2==null:e1.equals(e2)))换句话说,两个列表被定义为
如果它们包含相同顺序的相同元素,则表示相等。这
定义确保equals方法在多个应用程序中正常工作
列表接口的不同实现
您正在寻找:
预期和实际不引用同一对象的断言
您正在寻找:
预期和实际不引用同一对象的断言
比较参考文献:
assertTrue(model1.values != model2.values);
比较参考文献:
assertTrue(model1.values != model2.values);
为了满足您的需求,您必须将对对象引用的断言与对对象内容的断言结合起来 请注意,此关于集合内容的断言不够有力:
assertEquals(model1.values.size(), model2.values.size());
Integer value1 = model1.values.get(0);
Integer value2 = model2.values.get(0);
assertEquals(value1, value2);
仅断言集合的第一个元素显然是不够的,但是如果您断言第一个元素,那么您希望克隆的集合中只有一个元素。事实并非如此。相反,您应该依赖集合的
equals()
方法,该方法将equals()
应用于元素集
// Create the original value
Model model1 = ...
// Copy the original value into a new object
Model model2 = modelCopier(model1);
// 1) Check that they don't refer the same object
Assert.assertNotSame(model1, model2);
// 2) Check that they don't refer the same object
Assert.assertNotSame(model1.values, model2.values);
// 3) Check that their values properties contain the same values though!
Assert.assertEquals(model1.values, model2.values);
使用Assert.assertNotSame()
断言Model
和Model.values
不引用同一对象。然后使用
Assert.assertEqual()
断言Model.values
集合在包含的元素方面是相等的
// Create the original value
Model model1 = ...
// Copy the original value into a new object
Model model2 = modelCopier(model1);
// 1) Check that they don't refer the same object
Assert.assertNotSame(model1, model2);
// 2) Check that they don't refer the same object
Assert.assertNotSame(model1.values, model2.values);
// 3) Check that their values properties contain the same values though!
Assert.assertEquals(model1.values, model2.values);
为了满足您的需求,您必须将对对象引用的断言与对对象内容的断言结合起来 请注意,此关于集合内容的断言不够有力:
assertEquals(model1.values.size(), model2.values.size());
Integer value1 = model1.values.get(0);
Integer value2 = model2.values.get(0);
assertEquals(value1, value2);
仅断言集合的第一个元素显然是不够的,但是如果您断言第一个元素,那么您希望克隆的集合中只有一个元素。事实并非如此。相反,您应该依赖集合的
equals()
方法,该方法将equals()
应用于元素集
// Create the original value
Model model1 = ...
// Copy the original value into a new object
Model model2 = modelCopier(model1);
// 1) Check that they don't refer the same object
Assert.assertNotSame(model1, model2);
// 2) Check that they don't refer the same object
Assert.assertNotSame(model1.values, model2.values);
// 3) Check that their values properties contain the same values though!
Assert.assertEquals(model1.values, model2.values);
使用Assert.assertNotSame()
断言Model
和Model.values
不引用同一对象。然后使用
Assert.assertEqual()
断言Model.values
集合在包含的元素方面是相等的
// Create the original value
Model model1 = ...
// Copy the original value into a new object
Model model2 = modelCopier(model1);
// 1) Check that they don't refer the same object
Assert.assertNotSame(model1, model2);
// 2) Check that they don't refer the same object
Assert.assertNotSame(model1.values, model2.values);
// 3) Check that their values properties contain the same values though!
Assert.assertEquals(model1.values, model2.values);
谢谢这是愚蠢的简单。大家一定要阅读文档(谢谢!这太简单了。一定要读文档!:(