什么问题/陷阱,必须重写时,必须考虑equals和hashCode?
根据文档,Array.include?在对象上使用==比较.我来自Java,其中(通常)使用.equals()完成这些事情,这很容易覆盖特定对象.
如何在Ruby中覆盖==以允许我指定Array.include的行为?对于我的特定对象?
非常感谢.
public class Account {
String account;
double balance;
Account(String account, double balance) {
this.account = account;
this.balance = balance;
}
}
public class AccountTest {
public static void main(String[] args) {
Account a1 = new Account("Sandy", 1000);
Account a2 = new Account("Sandy", 1000);
System.out.println(a1.equals(a2));
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行它显示"false"但对象包含变量中的相同数据时.解释.
我有以下对象,
public class Pair {
private int row;
private int col;
public Pair(int row, int col){
this.row = row;
this.col = col;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
}
Run Code Online (Sandbox Code Playgroud)
我将这些对存储在一个队列中,但是不想检查Queue是否已包含该对.这是我的代码.
Queue<Pair> queue = new LinkedList<>();
if(!queue.contains(new Pair(curr.getRow(), curr.getCol()){
//do something
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,Queue正在存储重复值.有人可以帮助我理解为什么以及解决它的方法是什么?
ArrayList equals()在其contains方法中使用,以查看提供对象是否等于列表中的任何项目,如文档所示:
如果此列表包含指定的元素,则返回true.更正式地,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e)).看到这个
我有这门课
class Foo
{
private String value;
public Foo(String value)
{
this.value = value;
}
@Override
public boolean equals(Object o)
{
return o == null ? this.value == null : o.toString().equals(this.value);
}
}
Run Code Online (Sandbox Code Playgroud)
我想用contains方法检查项目是否存在这样
List<Foo> list = new ArrayList<Foo>();
Foo item1 = new Foo("item1");
Foo item2 = new Foo("item2");
list.add(item1);
list.add(item2);
System.out.println(item1.equals("item1")); //return true
System.out.println(list.contains("item1")); //false !! why?!
Run Code Online (Sandbox Code Playgroud)
但contains方法返回false,而item1.equals("item1")返回true. …
我正在使用下面的测试方法来检查 Arraylist 是否按预期排序。
@Test
void compareFields()
{
Assignment14 assignment14 = new Assignment14();
ArrayList<Person> PeopleList = new ArrayList<Person>();
PeopleList.add(new Person("Alex",56));
PeopleList.add(new Person("Thomas",23));
PeopleList.add(new Person("John",10));
ArrayList<Person> actualSortedResult = assignment14.compareFields(PeopleList);
ArrayList<Person> expectedSortedResult = new ArrayList<Person>();
expectedSortedResult.add(new Person("Alex",56));
expectedSortedResult.add(new Person("John",10));
expectedSortedResult.add(new Person("Thomas",23));
Assert.assertEquals(expectedSortedResult, actualSortedResult);
}
Run Code Online (Sandbox Code Playgroud)
但是,尽管预期和实际情况相似,但作为研究员却出现了错误。
java.lang.AssertionError: expected: java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]> but was: java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
Expected :java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
Actual :java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
<Click to see difference>
Run Code Online (Sandbox Code Playgroud)
我尝试过以下断言类型,但没有任何效果。
assertTrue("check equality", Arrays.equals(expectedSortedResult.toArray(), …Run Code Online (Sandbox Code Playgroud) 在java中使用indexOf()函数时,我应该覆盖哪个函数.我有一个数组列表,然后我接受一个输入作为ID并创建一个包含ID的对象,所有其他元素都为null,然后我需要传递该对象并获取包含该对象的元素的索引