ตัวอย่างการเรียงลำดับ Key ที่เป็นตัวอักษร
package demo.loop;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortMapOnKeyStringExample {
/**
* @param args
*/
public static void main(String[] args) {
Map<string string=""> unsortMap = new HashMap<string string="">();
unsortMap.put("Z", "z");
unsortMap.put("B", "b");
unsortMap.put("A", "a");
unsortMap.put("C", "c");
unsortMap.put("D", "d");
unsortMap.put("E", "e");
unsortMap.put("Y", "y");
unsortMap.put("N", "n");
unsortMap.put("J", "j");
unsortMap.put("M", "m");
unsortMap.put("F", "f");
System.out.println("Unsort Map......");
for (Map.Entry<string string=""> entry : unsortMap.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
System.out.println("\nSorted Map......");
Map<string string=""> treeMap = new TreeMap<string string="">(unsortMap);
for (Map.Entry<string string=""> entry : treeMap.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
}
}
ผลลัพธ์ที่ได้คือ
Unsort Map...... Key : D Value : d Key : E Value : e Key : F Value : f Key : A Value : a Key : B Value : b Key : C Value : c Key : M Value : m Key : N Value : n Key : Y Value : y Key : J Value : j Key : Z Value : z Sorted Map...... Key : A Value : a Key : B Value : b Key : C Value : c Key : D Value : d Key : E Value : e Key : F Value : f Key : J Value : j Key : M Value : m Key : N Value : n Key : Y Value : y Key : Z Value : z
ตัวอย่างการเรียงลำดับ Key ที่เป็นตัวเลข
package demo.loop;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortMapOnKeyIntegerExample {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer, String> unsortMap = new HashMap<Integer, String>();
unsortMap.put(10, "z");
unsortMap.put(5, "b");
unsortMap.put(6, "a");
unsortMap.put(20, "c");
unsortMap.put(1, "d");
unsortMap.put(7, "e");
unsortMap.put(8, "y");
unsortMap.put(99, "n");
unsortMap.put(50, "j");
unsortMap.put(2, "m");
unsortMap.put(9, "f");
System.out.println("Unsort Map......");
for (Map.Entry<Integer, String> entry : unsortMap.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
System.out.println("\nSorted Map......");
Map<Integer, String> treeMap = new TreeMap<Integer, String>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
treeMap.putAll(unsortMap);
for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
}
}
ผลลัพธ์ที่ได้คือ
Unsort Map...... Key : 50 Value : j Key : 1 Value : d Key : 2 Value : m Key : 99 Value : n Key : 20 Value : c Key : 5 Value : b Key : 6 Value : a Key : 7 Value : e Key : 8 Value : y Key : 9 Value : f Key : 10 Value : z Sorted Map...... Key : 99 Value : n Key : 50 Value : j Key : 20 Value : c Key : 10 Value : z Key : 9 Value : f Key : 8 Value : y Key : 7 Value : e Key : 6 Value : a Key : 5 Value : b Key : 2 Value : m Key : 1 Value : d
0 comments:
Post a Comment