SortedMap接口主要提供有序的Map實現。
Map的主要實現有HashMap,TreeMap,HashTable,LinkedHashMap。
TreeMap實現了SortedMap接口,保證了有序性。默認的排序是根據key值進行升序排序,也可以重寫comparator方法來根據value進行排序。
HashMap與TreeMap的比較
-
public
class SortedMapTest2 {
-
-
public static void main(String[] args) {
-
-
Map<String,Object> hashMap =
new HashMap<String,Object>();
-
hashMap.put(
"1",
"a");
-
hashMap.put(
"5",
"b");
-
hashMap.put(
"2",
"c");
-
hashMap.put(
"4",
"d");
-
hashMap.put(
"3",
"e");
-
Set<Entry<String, Object>> entry = hashMap.entrySet();
-
for(Entry<String, Object> temp : entry){
-
System.out.println(
"hashMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
-
System.out.println(
"\n");
-
-
SortedMap<String,Object> sortedMap =
new TreeMap<String,Object>();
-
sortedMap.put(
"1",
"a");
-
sortedMap.put(
"5",
"b");
-
sortedMap.put(
"2",
"c");
-
sortedMap.put(
"4",
"d");
-
sortedMap.put(
"3",
"e");
-
Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
-
for(Entry<String, Object> temp : entry2){
-
System.out.println(
"sortedMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
-
}
-
-
}
運算的結果為
-
hashMap:1 值a
-
hashMap:2 值c
-
hashMap:3 值e
-
hashMap:4 值d
-
hashMap:5 值b
-
-
sortedMap:1 值a
-
sortedMap:2 值c
-
sortedMap:3 值e
-
sortedMap:4 值d
-
sortedMap:5 值b
看上去還以為HashMap也保證了有序性,其實是隨機的,如果值設置的復雜一點,如下例:
-
public
class SortedMapTest3 {
-
-
public static void main(String[] args) {
-
-
Map<String,Object> hashMap =
new HashMap<String,Object>();
-
hashMap.put(
"1b",
"a");
-
hashMap.put(
"2",
"b");
-
hashMap.put(
"4b",
"d");
-
hashMap.put(
"3",
"c");
-
hashMap.put(
"2b",
"d");
-
hashMap.put(
"3b",
"c");
-
Set<Entry<String, Object>> entry = hashMap.entrySet();
-
for(Entry<String, Object> temp : entry){
-
System.out.println(
"hashMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
-
System.out.println(
"\n");
-
-
SortedMap<String,Object> sortedMap =
new TreeMap<String,Object>();
-
sortedMap.put(
"1b",
"a");
-
sortedMap.put(
"2",
"b");
-
sortedMap.put(
"4b",
"d");
-
sortedMap.put(
"3",
"c");
-
sortedMap.put(
"2b",
"d");
-
sortedMap.put(
"3b",
"c");
-
Set<Entry<String, Object>> entry2 = sortedMap.entrySet();
-
for(Entry<String, Object> temp : entry2){
-
System.out.println(
"sortedMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
-
}
-
-
}
運算的結果是:
-
hashMap:2b 值d
-
hashMap:1b 值a
-
hashMap:2 值b
-
hashMap:3 值c
-
hashMap:4b 值d
-
hashMap:3b 值c
-
-
sortedMap:1b 值a
-
sortedMap:2 值b
-
sortedMap:2b 值d
-
sortedMap:3 值c
-
sortedMap:3b 值c
-
sortedMap:4b 值d
很顯然只有TreeMap保證了有序性。
那如果想要根據value值來進行排序
-
public
class SortedMapTest {
-
-
public static void main(String[] args) {
-
-
SortedMap<String,String> sortedMap =
new TreeMap<String,String>();
-
sortedMap.put(
"1",
"a");
-
sortedMap.put(
"5",
"b");
-
sortedMap.put(
"2",
"c");
-
sortedMap.put(
"4",
"d");
-
sortedMap.put(
"3",
"e");
-
Set<Entry<String, String>> entry2 = sortedMap.entrySet();
-
for(Entry<String, String> temp : entry2){
-
System.out.println(
"修改前 :sortedMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
System.out.println(
"\n");
-
-
//這里將map.entrySet()轉換成list
-
List<Map.Entry<String,String>> list =
-
new ArrayList<Map.Entry<String,String>>(entry2);
-
-
Collections.sort(list,
new Comparator<Map.Entry<String,String>>(){
-
-
@Override
-
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
-
// TODO Auto-generated method stub
-
return o1.getValue().compareTo(o2.getValue());
-
}
-
-
});
-
-
for(Map.Entry<String,String> temp :list){
-
System.out.println(
"修改后 :sortedMap:"+temp.getKey()+
" 值"+temp.getValue());
-
}
-
}
-
-
}
運行結果為:
-
修改前 :sortedMap:1 值a
-
修改前 :sortedMap:2 值c
-
修改前 :sortedMap:3 值e
-
修改前 :sortedMap:4 值d
-
修改前 :sortedMap:5 值b
-
-
修改后 :sortedMap:1 值a
-
修改后 :sortedMap:5 值b
-
修改后 :sortedMap:2 值c
-
修改后 :sortedMap:4 值d
-
修改后 :sortedMap:3 值e