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.
We can get an address using the id() function. id() function gives the address of the particular object.
USPS Address DatabaseThe United States Postal Service maintains a database of 160 million mailing addresses in the United States that is updated monthly. The database contains business, residential, and post office box delivery points.
I guess you want
System.identityHashCode(list);
the javadoc says
Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With