我在找一种方法 重新命名哈沙马普键, 但我不知道它是否可能 在爪哇。
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 ...
我在找一种方法 重新命名哈沙马普键, 但我不知道它是否可能 在爪哇。
尝试删除元素, 然后用新名称将其重新放入。 假设您的地图中的密钥是 < code> string code >, 就可以实现 :
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 code> 。
唯一的办法是删除/删除 key
,并插入新的 key
和 value
对。
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))
);
请参看以下各点:
否,您无法在添加后重新命名 key
的 Hashmap
。
首先,你必须删除或删除该 key
,然后用新的 keys
插入 keys
,加上 value
。
因为在 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));
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...