Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an address of a List?

Tags:

java

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.

like image 527
Andrey Chaschev Avatar asked Dec 17 '13 15:12

Andrey Chaschev


People also ask

How do I get an address in Python?

We can get an address using the id() function. id() function gives the address of the particular object.

Does USPS have a database of addresses?

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.


1 Answers

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().

like image 91
René Link Avatar answered Sep 19 '22 20:09

René Link