gpt4 book ai didi

java - 当键被覆盖时 LinkedHashMap#put 是否保持顺序? (JAVA)

转载 作者:行者123 更新时间:2023-12-02 12:40:22 24 4
gpt4 key购买 nike

这里是一个 LinkedHashMap,想象它里面有很多值。

LinkedHashMap 映射 = new LinkedHashMap<>();

我的问题是,如果现有值之一位于索引 4 处,并且相同的键被 .put 到映射中,它是否会简单地替换该值并保持为 4?或者,它会添加到最后吗?

最佳答案

documentation of LinkedHashMap明确指出这一点:

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

因此值被替换,但迭代顺序将保持不变。对于您的示例,带有键 4 的条目将在迭代中保持在相同位置。

关于java - 当键被覆盖时 LinkedHashMap#put 是否保持顺序? (JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com