I was going through this question on creating a HashTable<K,V> as final. I read this answer where we use Collections.unmodifiableMap to put an un-modifiable wrapper over the hash table. This got me wondering if there is a HashMap implementation or any other similar structure, where I can insert whenever I want but once I've inserted a key and value, another insertion on the same key is not allowed.
For eg:
private final HashMap<Integer, Integer> test = new HashMap<>();
test.put(1, 100);
test.put(2, 200);
/*
* Other insertions
*/
test.put(1, 50); //This should not be allowed
One solution which I can think of is to do:
if(test.get(1) == null) {
test.put(1, 50); //Similarly for all insertions
}
or to create a method which will do this check, like:
private void putIfNotPresent(int key, int value) {
if(test.get(key) == null) { //Considering that null will be returned if there is no mapping available for the key
test.put(key, value);
} else {
throw new UnsupportedOperationException;
}
}
I'm also aware of the putIfAbsent(K, V) method, but this also will not disallow if someone tries to put() some value for an already existing key.
All the solutions which I've come up with won't stop someone from inserting into the HashMap if the key is already present, unless they use the putIfNotPresent() custom method or the putIfAbsent method for every insertion. Is there such a HashMap implementation available where once a key and value is inserted, insertion on the same key is disallowed i.e. once a key is inserted with a value, it should act as if the key's value is final?