Problem statement:
Write the java code to sort the values in HashMap.
Write the java code to sort the values in HashMap.
- import java.util.Collection;
- import java.util.HashMap;
- 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");
- Collection<String> c = m.values();
- c = new TreeSet<String>(c);
- System.out.println(c);
- }
- }
Output:
[aa, bb, cc, zz]
No comments:
Post a Comment