0

Hello.

Like as the title sayes. Im trying to edit key values in a Map/hashMap/treeMap.

For example:

public class MapExample {

    public static void main(String[] args) {

        Map<int,String> mp=new HashMap<int, String>();

        // adding or set elements in Map by put method key and value pair
        mp.put(12, "Two");
        mp.put(11, "One");
        mp.put(13, "Three");
        mp.put(14, "Four");

      }
}

I want to edit it to

mp.put(2, "Two");
        mp.put(1, "One");
        mp.put(3, "Three");
        mp.put(4, "Four");

I was chacking java API but i haven't finde anything useful.
Thanks for help.

3
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by NormR1
0

well, you could remove all the items, and store them back into the Map with the new key values, or just creating a new Map, which overwrites the old one, and which you fill with the new values.

0

Ineed something like:

i=0

while( hashMap.hasNext() ){
[fakecode]iterator it[/fakecode] //iterates the hashMap

hashMap.changeKeyValueTo(i)

i++



}
0

To "change" a key means that the old key is not in the Map any more and the new key points to the data the old key pointed to.
Basic steps would be get the value for the old key, delete the old key, store data with new key.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.