But the OP isn't writing the same String literal twice. It doesn't matter whether you use the String pool or not.
sohail hussain wrote:. . . if you create string literal it is stored in string literal pool, so again if you create the same literal object . . .
That makes it look as if you were guessing; you can guess 1,000,000 times and some of the guesses will be correct. Or you can think about the problem and get it right first time.
arwa ali wrote:below the code that I tried so far but it is not working well . . .
No, you are correct. A hash map doesn't have any concept about the order of its K‑V pairs. So you are going to have to use something different if you want to retain insertion order and replace a K in the same position as its predecessor was. Remember that if your map already contains that K, calling put() a V against that K, you will have something completely different happen (I think you know that already).
James Mac wrote:. . . to replace a key you need to first remove the key that is going to be replace and put a new one. But that doesn't save the position of the removed key and value. . . .