Search code examples
javamethodsgetkeytreemap

Java TreeMap get method


When i use the V get(Object key) method in a Java TreeMap<K, V> I obtain a brand new copy of the value connected to the key I insert in the V get(Object key) method, or I got a "reference" to the value V right inside the TreeMap?


Solution

  • The Map#get() returns a reference but the Map#put() method too stores a reference only. The actual Object lives on the heap. In fact, the TreeMap works with Entry<K,V> references and the actual Entry objects on the heap then point to the actual key and value objects on the heap again.