How can I get an address of a List?

I need to check if a returned list was created once or if it's a copy of an object. Is it possible to find out it's 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));

How could getAddress(list) look like? The problem is that hashCode() which normally returns an address is overridden in AbstractList, so it would return a valid hash code instead of an address.

Jon Skeet
people
quotationmark

Is it possible to find out it's address?

No. You can, however, check for reference equality - which is all you care about:

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

If you really want to do this from logging, you can use System.identityHashCode instead, but be aware that this is not meant to be deemed an address. It's still a hash code, and is not guaranteed to be unique. It may be related to some internal address, but that's far from guaranteed too.

people

See more on this question at Stackoverflow