Java LinkedHashMap替换键 [英] Java LinkedHashMap replace key

查看:692
本文介绍了Java LinkedHashMap替换键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在LinkedHashMap中使用put()替换密钥而不丢失密钥最初插入的顺序吗?

Is there any way to replace a key using put() in a LinkedHashMap without losing the order that the key was originally inserted in?

推荐答案

如果您使用LinkedHashMap,我认为没有内置的方法可以实现您的目标.您可能想要选择另一个(或设计自己的)数据结构.

If you used the LinkedHashMap, I don't think there is built-in method to achieve your goal. You may want to pick another (or design your own) data-structure.

如果必须在linkedhashmap上执行此操作,则可以创建一个新的LinkedHashMap,将旧的迭代并放入新的,当目标条目出现时,使用不同的密钥创建一个新条目,然后将新条目放入地图.

If you have to do it on a linkedhashmap, you can create a new LinkedHashMap, iterate the old one and put into the new one, when your target entry comes, create a new entry with different key, put the new entry into the map.

这篇关于Java LinkedHashMap替换键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆