Problem statement:
Write the java code to sort the keys in HashMap.
[key12, key27, key39, key45]
Write the java code to sort the keys in HashMap.
- import java.util.HashMap;
- import java.util.Set;
- import java.util.TreeSet;
- public class Test {
- public static void main(String[] args) {
- HashMap<String, String> m = new HashMap<String, String>();
- m.put("key45", "aa");
- m.put("key12", "zz");
- m.put("key39", "cc");
- m.put("key27", "bb");
- Set<String> s = m.keySet();
- s = new TreeSet<String>(s);
- System.out.println(s);
- }
- }
[key12, key27, key39, key45]
No comments:
Post a Comment