0

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.

1
  • The entries in HashMaps aren't ordered (the "normal" ordered, that is). You're going to need to do some sorting work first on the keys/entries. Commented Jun 23, 2014 at 5:08

2 Answers 2

1

Use a TreeMap object instead of HashMap.

Iterating on a TreeMap will give you the keys in ascending order. Just push the values in a stack as you iterate and then pop them one by one then voila, you now have your keys in descending order.

TreeMap<String, String> mapp = new TreeMap<String, String>();
mapp.put("ab","blue");
mapp.put("abc","black");
mapp.put("abcdef","green");
mapp.put("abcd","pink");

Stack<Entry<String, String>> stk = new Stack<Entry<String, String>>();
for (Entry<String, String> entry : mapp.entrySet()) {
    stk.push(entry);
}

while (!stk.isEmpty()) {
    Entry<String, String> entry = stk.pop();
    // Do something with entry
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for reply.but can you give me sample code as per my code?
This is not a good approach; why do you need to store again in a stack ?
What is wrong about using a stack? Well, another solution would be to pass a Comparator.
0

Can be done directly through TreeMap; you just need to pass on your own Comparator which sorts keys by length in descending order.

    Map<String,String> mapp = new TreeMap<String,String>(new Comparator<String>(){
        @Override
        public int compare(String paramT1, String paramT2) {
            Integer t1 = paramT1.length();
            Integer t2 = paramT2.length();
            return t2.compareTo(t1);
        }

    });
     mapp.put("ab","blue");
     mapp.put("abc","black");
     mapp.put("abcdef","green");
     mapp.put("abcd","pink");

     for(String t : mapp.keySet()){
         System.out.println(" key :"+ t);
     }

I have discussed about Comparators in detail on my blog; here.

2 Comments

can it will give greatest value in descending order?
yeah it gives; you can test it. Just copy and paste above code in the main method and run it. Approach suggested by @Emanuel is not proper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.