String compression. In the test case s and d swap automatically inside hasmap. how to prevent this?

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner obj=new Scanner(System.in);
String s=obj.next();
String ans="";
HashMap<Character,Integer> map=new HashMap<>();
for(int i=0;i<s.length();i++){
char a=s.charAt(i);
// System.out.println(a);
if(map.containsKey(a)){
map.put(a,map.get(a)+1);
}
else
map.put(a,1);
}
for (Map.Entry<Character,Integer> entry : map.entrySet())
{ ans=ans+entry.getKey();
if(entry.getValue()!=1){
ans=ans+entry.getValue();
}

   }
   System.out.println(ans);
}

}

using a map in this question might not be the best option because map stores a unique value corresponding every key…so for a string like “aaabcaa” your map will have entries :
a-5,b-1,c-1…
moreover the order in which you put entries in a map may not be the same when you try to get those entries…it all depends on the hashcode function…so instead of using a map you could do it using iteration…

Hey Kush,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.