Problem statement:
How do you sort alphanumeric key of a map ?
How do you sort alphanumeric key of a map ?
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- public class SortAlphanumericKeyInMap {
- public static void main(String[] args) {
- Map<String, String> map = new HashMap<>();
- map.put("A1BB", "1");
- map.put("A2CC", "2");
- map.put("A2AA", "2");
- map.put("B1AA", "7");
- Map<String, String> treeMap = new TreeMap<>(map);
- Set<String> keys = treeMap.keySet();
- for (String key : keys) {
- System.out.println(key);
- }
- }
- }
Output:
A1BB
A2AA
A2CC
B1AA
No comments:
Post a Comment