How can I get the list address?

I need to check if the returned list was created once or if it is a copy of the object. Can I find out his address?

// thread 1 List<Object> list = supplier.get(); System.out.print("list: " + list + "@" + getAddress(list)); // thread 2 List<Object> list = supplier.get(); System.out.print("list: " + list + "@" + getAddress(list)); 

What does getAddress(list) look like? The problem is that hashCode() , which usually returns the address, is overridden by AbstractList , so it returns the correct hash code instead of the address.

+7
java
source share
3 answers

Can I find out his address?

Not. However, you can check referential equality - that’s all you care about:

 if (list1 == list2) { // They're the same } 

If you really want to do this during registration, you can use System.identityHashCode , but keep in mind that this should not be considered an address. It is still a hash code and is not guaranteed to be unique. This may be due to some kind of internal address, but this is far from guaranteed.

+6
source share

I think you want

  System.identityHashCode(list); 

javadoc says

Returns the same hash code for this object, which will be returned by the hashCode () method by default, regardless of whether or not this class of the hashCode () object overrides.

+8
source share

The object address is not fixed in Java, it can move, so you cannot get it.

Why don't you just check if(list1 == list2) ?

+2
source share

All Articles