简体   繁体   English

Java TreeMap的get方法

[英]Java TreeMap get method

当我在Java TreeMap<K, V>使用V get(Object key)方法时TreeMap<K, V>我获得了与插入到V get(Object key)方法中的键相关的值的全新副本,或者得到了“在TreeMap内部直接指向值V?

The Map#get() returns a reference but the Map#put() method too stores a reference only. Map#get()返回引用,Map#put()方法也仅存储引用 The actual Object lives on the heap . 实际的Object存在于堆中 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. 事实上, TreeMap与工作Entry<K,V>参考和实际Entry堆中的对象然后再次指向在堆上的实际 的对象。

In general, Java's standard collections do not copy the values they are given. 通常,Java的标准集合不会复制它们给定的值。 If you notice, the values are not constrained to be Cloneable (and even if they were, because of the weirdness of Cloneable , it wouldn't ensure that they were actually clonable). 如果您注意到,则这些值并不限于可克隆(即使它们是可Cloneable ,由于Cloneable的怪异,也无法确保它们实际上是可克隆的)。 And there is no other way for a copy to be created (since you cannot run new on a generic type variable). 并且没有其他方法可以创建副本(因为无法在泛型类型变量上运行new )。

Therefore, they have to make do with the references they were given, and that's what you get when you retrieve them with get() . 因此,它们必须处理给定的引用,这就是使用get()检索它们时所get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM