map是键值对的集合接口,它的实现类主要包括:hashmap,treemap,hashtable以及linkedhashmap等。其中这四者的区别如下(简单介绍):
hashmap:我们最常用的map,hashmap的值是没有顺序的,他是按照key的hashcode来实现的,就是根据key的hashcode 值来存储数据,根据key可以直接获取它的value,同时它具有很快的访问速度。hashmap最多只允许一条记录的key值为null(多条会覆盖);允许多条记录的value为 null。非同步的。
treemap: 能够把它保存的记录根据key排序,默认是按升序排序,也可以指定排序的比较器,当用iterator 遍历treemap时,得到的记录是排过序的。treemap不允许key的值为null。非同步的。
hashtable: 与 hashmap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写hashtable,因此也导致了hashtale在写入时会比较慢,只有hashtable是继承自dictionary抽象类的,hashmap和treemap都继承自abstractmap抽象类,linkedhashmap继承自hashmap。
linkedhashmap: 保存了记录的插入顺序,在用iterator遍历linkedhashmap时,先得到的记录肯定是先插入的.在遍历的时候会比hashmap慢。key和value均允许为空,非同步的。
常识:
collection与map集合是不是继承自object?
不是,两个都是接口,object是类,怎么可能会继承自object,详细看java.util下的具体接口。
二、map排序
treemap
treemap默认是升序的,如果我们需要改变排序方式,则需要使用比较器:comparator。
comparator可以对集合对象或者数组进行排序的比较器接口,实现该接口的public compare(t o1,to2)方法即可实现排序,该方法主要是根据第一个参数o1,小于、等于或者大于o2分别返回负整数、0或者正整数。如下:
public class treemaptest { public static void main(string[] args) { map<string, string> map = new treemap<string, string>( new comparator<string>() { public int compare(string obj1, string obj2) { // 降序排序 return obj2.compareto(obj1); } }); map.put("c", "ccccc"); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("d", "ddddd"); 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的key值来进行排序的,但是有时我们需要根据treemap的value来进行排序。对value排序我们就需要借助于collections的sort(list<t> list, comparator<? super t> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件,那就是所有的元素都必须能够根据所提供的比较器来进行比较。如下:
public class treemaptest { public static void main(string[] args) { map<string, string> map = new treemap<string, string>(); map.put("d", "ddddd"); map.put("b", "bbbbb"); map.put("a", "aaaaa"); map.put("c", "ccccc"); //这里将map.entryset()转换成list list<map.entry<string,string>> list = new arraylist<map.entry<string,string>>(map.entryset()); //然后通过比较器来实现排序 collections.sort(list,new comparator<map.entry<string,string>>() { //升序排序 public int compare(entry<string, string> o1, entry<string, string> o2) { return o1.getvalue().compareto(o2.getvalue()); } }); for(map.entry<string,string> mapping:list){ system.out.println(mapping.getkey()+":"+mapping.getvalue()); } }}
我们都是hashmap的值是没有顺序的,他是按照key的hashcode来实现的。对于这个无序的hashmap我们要怎么来实现排序呢?参照treemap的value排序,我们一样的也可以实现hashmap的排序。
public class hashmaptest { public static void main(string[] args) { map<string, string> map = new hashmap<string, string>(); map.put("c", "ccccc"); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("d", "ddddd"); list<map.entry<string,string>> list = new arraylist<map.entry<string,string>>(map.entryset()); collections.sort(list,new comparator<map.entry<string,string>>() { //升序排序 public int compare(entry<string, string> o1, entry<string, string> o2) { return o1.getvalue().compareto(o2.getvalue()); } }); for(map.entry<string,string> mapping:list){ system.out.println(mapping.getkey()+":"+mapping.getvalue()); } }}
以上就是4个主要的map实现类的详细内容。