Gosh, i hope not. deleting and reentering the key/value pair seems like the way to go. note that you normally just handle references in the map itself anyway.. How to update a value in a map if it exists else insert it. ask question. associates the specified value with the specified key in this map. if the map previously contained a mapping for the key, the old value is replaced. so you only need . mymap.put(key, value); how to update a value, given a key in a java hashmap? 3.. Java map interface. a map contains values on the basis of key, i.e. key and value pair. each key and value pair is known as an entry. a map contains unique keys. a map is useful if you have to search, update or delete elements on the basis of a key. java map hierarchy.
On this document we will be showing a java example on how to use the replace() method of hashmap class.basically this method is being used to insert a new a new key-value mapping to the hashmap object.. You can’t change the key object in a key-value pair to be a different object. if you think about what a map does — maps keys to values — that would be a fairly meaningless thing to do. you can however always add a new key pointing to the same value object and then delete the old one, which would have much the same effect.. Download run code. 3. create reverse map. the idea is extend hashmap class and overload its put() method such that it also inserts the value-key pair into a reverse map along with key-value pair in the original map. we also create a getkey() method that facilitates the value lookup in reverse map..