简体   繁体   English

Java对象比较和带有对象键的哈希表

[英]Java object comparison and Hashtable with Object keys

Suppose I have an MyObject class, and it's stored in 2 different ArrayList . 假设我有一个MyObject类,它存储在2个不同的ArrayList If get the same object from the 2 ArrayList, how would I compare them to check if they're identical? 如果从2 ArrayList获得相同的对象,我将如何比较它们以检查它们是否相同?

Would I do instance1 == instance2 or instance1.equals(instance2) ? 我会执行instance1 == instance2还是instance1.equals(instance2)吗? I'm pretty sure the == operator looks at the pointer location, but if I get instance1 from array1 and instance2 from array2, would they return different pointer locations or the same? 我很确定==运算符会查看指针位置,但是如果我从array1获得instance1和从array2获得instance2,它们将返回不同的指针位置还是相同的?

Same thing with Hashtable: Does it store a pointer location as the key or the actual object? 与Hashtable相同:它存储指针位置作为键还是实际对象? If I have 2 objects with identical content but are actually different objects, would it still work? 如果我有两个对象具有相同的内容,但实际上是不同的对象,它仍然可以工作吗?

Thanks 谢谢

if I get instance1 from array1 and instance2 from array2, would they return different pointer locations or the same? 如果我从array1获取instance1和从array2获取instance2,它们将返回不同的指针位置还是相同的指针位置?

Yes, if they are referring to the same object, == will return true. 是的,如果他们指的是同一个对象,则==将返回true。

An ArrayList will store precisely what you give to it. ArrayList将精确存储您提供的内容。 If you give it a reference ("address") it will store that address. 如果您给它一个参考(“地址”),它将存储该地址。 When you fetch the that particular element, you'll get the same reference back. 当您获取该特定元素时,您将获得相同的引用。

Sample snippet: 样本片段:

Object o = new Object();

List<Object> l1 = new ArrayList<Object>();
List<Object> l2 = new ArrayList<Object>();

l1.add(o);
l2.add(o);

Object o1 = l1.get(0);
Object o2 = l2.get(0);

System.out.println(o1 == o2); // prints true

In a sense the ArrayList does however make a copy (of the reference!) As this snippet illustrates, the list is not affected when changing s : 从某种意义上讲,ArrayList 确实做了(引用的副本!)如该片段所示,更改s时,列表不受影响:

String s = "hello";
List<String> l = new ArrayList<String>();
l.add(s);
s = "world";
System.out.println(l.get(0));  // prints hello

Basically == compares content of variables. 基本上==比较变量的内容。 Since the content of a reference is an "address", comparing references using == will yield true if and only if the two references refer to the same object. 由于引用的内容是一个“地址”,因此当且仅当两个引用引用相同的对象时,使用==进行比较的引用才会生成true。

o1.equals(o2) on the other hand compares the actual objects which o1 and o2 refer to. o1.equals(o2)比较o1o2引用的实际对象。


Does it store a pointer location as the key or the actual object? 它是否将指针位置存储为键或实际对象? If I have 2 objects with identical content but are actually different objects, would it still work? 如果我有两个对象具有相同的内容,但实际上是不同的对象,它仍然可以工作吗?

It uses the actual object as key. 它使用实际对象作为键。 If you have two separate objects which are equal , then you can use either one to retrieve the value stored for that key. 如果您有两个equal单独对象,则可以使用其中一个来检索为该键存储的值。


I'm pretty sure the == operator looks at the pointer location 我很确定==运算符会查看指针位置

This is nitpicking, but, no, == looks at pointer content , or object locations. 这很挑剔,但是不, ==查看指针的内容对象的位置。

You need to differentiate between objects and references . 您需要区分对象引用

Two different lists (or in general, any expressions) can be equal references to the same object. 两个不同的列表(或者通常是任何表达式)可以相等地引用同一对象。 For example: 例如:

MyObject x = new MyObject();
MyObject y = x;

Here x and y are separate variables but they both have values referring to the same object. 这里xy是单独的变量,但是它们都具有引用同一对象的值。 That "referring to the same object" is what == will compare. ==将比较“引用相同的对象”。

If you use equals then (assuming you've overridden equals in MyObject ) two different objects can still be equal. 如果使用equals那么(假设您在MyObject重写了equals ),两个不同的对象仍然可以相等。

For example: 例如:

Integer x = new Integer(1);
Integer y = new Integer(1);
System.out.println(x == y); // False
System.out.println(x.equals(y)); // True

Here the values of x and y are references to different objects, but those objects are considered to be equal. 这里的xy值是对不同对象的引用,但是这些对象被认为是相等的。

So, does your MyClass class have a notion of object equality beyond just "object identity"? 那么,您的MyClass类是否具有除“对象标识”之外的对象平等概念? Which aspect are you interested in when you compare the references stored in the lists? 比较列表中存储的参考文献时,您对哪个方面感兴趣?

If the same object has been stored in two separate ArrayList containers and you want to check whether they are identical, use the == operator to check for object identity. 如果同一对象已存储在两个单独的ArrayList容器中,并且您想检查它们是否相同,请使用==运算符检查对象身份。

This will work since only the references are stored in the ArrayList . 这将起作用,因为只有引用存储在ArrayList

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM