Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何检查两个ArrayList是否是唯一的对象(即使它们具有相同的值)?_Java_Arraylist_Collections - Fatal编程技术网

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);

谢谢这是愚蠢的简单。大家一定要阅读文档(谢谢!这太简单了。一定要读文档!:(