English 中文(简体)
可以重命名哈沙马键吗?
原标题:Is it possible to rename a Hashmap key?
  • 时间:2012-05-26 14:02:59
  •  标签:
  • java
  • hashmap

我在找一种方法 重新命名哈沙马普键, 但我不知道它是否可能 在爪哇。

最佳回答

尝试删除元素, 然后用新名称将其重新放入。 假设您的地图中的密钥是 < code> string , 就可以实现 :

Object obj = map.remove("oldKey");
map.put("newKey", obj);
问题回答
hashMap.put("New_Key", hashMap.remove("Old_Key"));

这将做你想做的事, 但是, 你会注意到密钥的位置已经改变 。

指定需要重新命名的密钥值为新密钥。并删除旧密钥。

hashMap.put("New_Key", hashMap.get("Old_Key"));
hashMap.remove("Old_Key");

您无法在添加后重命名/ 修改 hashmap < code>keys

唯一的办法是删除/删除 key ,并插入新的 key value 对。

Reason :在哈什马普内部实施中,Hashmap keys 修饰器被标记为 final

static class Entry<K ,V> implements Map.Entry<K ,V>
{
    final K key;
    V value;
    Entry<K ,V> next;
    final int hash;
    ...//More code goes here
}

参考文献:HashMap

您不重命名 hashmap 密钥, 您必须用新密钥插入一个新条目, 然后删除旧的密钥 。

我争论的是,仓储键的精髓是用于索引存取目的,仅此而已。 黑客就是一个黑客:围绕键值制作一个键包件类,使键包件对象成为索引存取的速成键,这样您就可以根据具体需要访问并更改键包件对象值:

public class KeyWrapper<T>{

      private T key;
      public KeyWrapper(T key){
         this.key=key;
       }

      public void rename(T newkey){
              this.key=newkey;
       }

}

示例示例示例示例

   HashMap<KeyWrapper,String> hashmap=new HashMap<>();
   KeyWrapper key=new KeyWrapper("cool-key");
   hashmap.put(key,"value");
   key.rename("cool-key-renamed");

虽然你也可以有一个非现有钥匙 能够从速速取得一个现有钥匙的价值 但我担心它可能是犯罪,反正:

public  class KeyWrapper<T>{

        private T key;
        public KeyWrapper(T key){
            this.key=key;
        }

        @Override
        public boolean equals(Object o) {
            return hashCode()==o.hashCode();
        }

        @Override
        public int hashCode() {
            int hash=((String)key).length();//however you want your hash to be computed such that two different objects may share the same at some point
            return hash;
        }
    }

示例示例示例示例

HashMap<KeyWrapper,String> hashmap=new HashMap<>();
KeyWrapper cool_key=new KeyWrapper("cool-key");
KeyWrapper fake_key=new KeyWrapper("fake-key");
hashmap.put(cool_key,"cool-value");
System.out.println("I don t believe it but its: "+hashmap.containsKey(fake_key)+" OMG!!!");

在我的例子中,我有一个地图,其中不是真实的密钥 - & gt; 真实的密钥, 所以我不得不用我的地图中的真实替换非真实的密钥( 这个想法和其他人一样)

getFriendlyFieldsMapping().forEach((friendlyKey, realKey) ->
    if (map.containsKey(friendlyKey))
        map.put(realKey, map.remove(friendlyKey))
);

请参看以下各点:

  1. 否,您无法在添加后重新命名 key Hashmap

  2. 首先,你必须删除或删除该 key ,然后用新的 keys 插入 keys ,加上 value

  3. 因为在 Hashmap 内部执行中, Hashmap 关键修饰符是 final

You can, if instead of Java native HashMap you ll use a Bimap.
It s not the traditional Map implementation, and you need to make sure it s suits your needs.

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

Using a bimap, you can inverse the view and replace the key.
Checkout both Apache Commons BidiMap and Guava BiMap.

有了更现代的爪哇,你就可以做

var new_map = old_map.entrySet().stream().collect(Collectors.toMap(e -> {
    // Rename key here
    return e.getKey() + "_postfix";
}, v -> v));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签