I want to iterate greatest key value first.My code is here. I have hashmap with some value . I want to iterate greatest key value first, so that it will replace greatest key value first as per my code.
HashMap<String, String> mapp=new HashMap<String, String>();
mapp.put("ab","blue");
mapp.put("abc","black");
mapp.put("abcdef","green");
mapp.put("abcd","pink");
for (Iterator it = alltyp.iterator(); it.hasNext();) {
String finalstring = (String) it.next();
Iterator it1=mapp.entrySet().iterator();
while(it1.hasNext())
{
Map.Entry pairs = (Map.Entry) it1.next();
String key_ = (String) pairs.getKey();
String value_ = (String) pairs.getValue();
finalstring = finalstring.replaceAll(key_, value_);
}
}
First it will iterate key value "abcdef" then "abcd" then "abc" and finaly "ab". Now in while loop it will replace "abcdef" first. please give me your suggetion.
HashMaps aren't ordered (the "normal" ordered, that is). You're going to need to do some sorting work first on the keys/entries.