简介:这是Java中对Map(HashMap,TreeMap,Hashtable等)的排序的详细页面,介绍了和java,有关的知识、技巧、经验,和一些java源码等。
首先简单说一下他们之间的区别:
hashmap: 最常用的map,它根据键的hashcode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。hashmap最多只允许一条记录的键为null(多条会覆盖);允许多条记录的值为 null。非同步的。
treemap: 能够把它保存的记录根据键(key)排序,默认是按升序排序,也可以指定排序的比较器,当用iterator 遍历treemap时,得到的记录是排过序的。treemap不允许key的值为null。非同步的。
hashtable: 与 hashmap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写hashtable,因此也导致了hashtale在写入时会比较慢。
linkedhashmap: 保存了记录的插入顺序,在用iterator遍历linkedhashmap时,先得到的记录肯定是先插入的.在遍历的时候会比hashmap慢。key和value均允许为空,非同步的。
treemap默认按key进行升序排序,如果想改变默认的顺序,可以使用比较器:
map<string,string> map = new treemap<string,string>(new comparator<string>(){
public int compare(string obj1,string obj2){
//降序排序
return obj2.compareto(obj1);
}
});
map.put("month", "the month");
map.put("bread", "the bread");
map.put("attack", "the attack");
set<string> keyset = map.keyset();
iterator<string> iter = keyset.iterator();
while(iter.hasnext()){
string key = iter.next();
system.out.println(key+":"+map.get(key));
}
public int compare(string obj1,string obj2){
//降序排序
return obj2.compareto(obj1);
}
});
map.put("month", "the month");
map.put("bread", "the bread");
map.put("attack", "the attack");
set<string> keyset = map.keyset();
iterator<string> iter = keyset.iterator();
while(iter.hasnext()){
string key = iter.next();
system.out.println(key+":"+map.get(key));
}
如果要对treemap按照value的值进行排序,或者对hashmap,hashtable,linkedhashmap进行排序,则可以使用map.entry<k,v>接口结合list实现:
eg.1 对treemap按照value值升序:
list<map.entry<string,string>> mappinglist = null;
map<string,string> map = new treemap<string,string>();
map.put("aaaa", "month");
map.put("bbbb", "bread");
map.put("ccccc", "attack");
//通过arraylist构造函数把map.entryset()转换成list
mappinglist = new arraylist<map.entry<string,string>>(map.entryset());
//通过比较器实现比较排序
collections.sort(mappinglist, new comparator<map.entry<string,string>>(){
public int compare(map.entry<string,string> mapping1,map.entry<string,string> mapping2){
return mapping1.getvalue().compareto(mapping2.getvalue());
}
});
for(map.entry<string,string> mapping:mappinglist){
system.out.println(mapping.getkey()+":"+mapping.getvalue());
}
map<string,string> map = new treemap<string,string>();
map.put("aaaa", "month");
map.put("bbbb", "bread");
map.put("ccccc", "attack");
//通过arraylist构造函数把map.entryset()转换成list
mappinglist = new arraylist<map.entry<string,string>>(map.entryset());
//通过比较器实现比较排序
collections.sort(mappinglist, new comparator<map.entry<string,string>>(){
public int compare(map.entry<string,string> mapping1,map.entry<string,string> mapping2){
return mapping1.getvalue().compareto(mapping2.getvalue());
}
});
for(map.entry<string,string> mapping:mappinglist){
system.out.println(mapping.getkey()+":"+mapping.getvalue());
}
eg.2 对hashmap(或hashtable,linkedhashmap)按照key的值升序:
list<map.entry<string,string>> mappinglist = null;
map<string,string> map = new hashmap<string,string>();
map.put("month", "month");
map.put("bread", "bread");
map.put("attack", "attack");
//通过arraylist构造函数把map.entryset()转换成list
mappinglist = new arraylist<map.entry<string,string>>(map.entryset());
//通过比较器实现比较排序
collections.sort(mappinglist, new comparator<map.entry<string,string>>(){
public int compare(map.entry<string,string> mapping1,map.entry<string,string> mapping2){
return mapping1.getkey().compareto(mapping2.getkey());
}
});
for(map.entry<string,string> mapping:mappinglist){
system.out.println(mapping.getkey()+":"+mapping.getvalue());
}
map<string,string> map = new hashmap<string,string>();
map.put("month", "month");
map.put("bread", "bread");
map.put("attack", "attack");
//通过arraylist构造函数把map.entryset()转换成list
mappinglist = new arraylist<map.entry<string,string>>(map.entryset());
//通过比较器实现比较排序
collections.sort(mappinglist, new comparator<map.entry<string,string>>(){
public int compare(map.entry<string,string> mapping1,map.entry<string,string> mapping2){
return mapping1.getkey().compareto(mapping2.getkey());
}
});
for(map.entry<string,string> mapping:mappinglist){
system.out.println(mapping.getkey()+":"+mapping.getvalue());
}