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.

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.

Ineed something like:

i=0

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

hashMap.changeKeyValueTo(i)

i++



}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.