对map集合进行排序-编程开发技术_第1页
对map集合进行排序-编程开发技术_第2页
对map集合进行排序-编程开发技术_第3页
对map集合进行排序-编程开发技术_第4页
对map集合进行排序-编程开发技术_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、对map集合进行排序-编程开发技术对map集合进行排序原文出处:chenssy?今天做统计时需要对x轴的地区按照地区代码(areacode)进行排序, 由于在构建xmldata使用的map來进行数据统计的,所以在统计过程屮 就需要对map进行排序。一、简单介绍map?在讲解map排序之前,我们先来稍微了解下map。map是键值对 的集合接口,它的实现类主要包括:hashmap, treemap, hashtable以及 linkedllashmap等。其中这四者的区别如下(简单介绍):? hashmap:我们最常用的map,它根据key的hashcode值来存 储数据,根据key可以直接获取它

2、的value,同时它具有很快的访问速 度。hashmap最多只允许一条记录的key值为null (多条会覆盖);允许 多条记录的value为null。非同步的。? treemap:能够把它保存的记录根据key排序,默认是按升序排 序,也可以指定排序的比较器,当用iterator遍历treemap时,得到 的记录是排过序的。treemap不允许key的值为null。非同步的。? hashtable:与hashmap类似,不同的是:key和value的值均不 允许为null;它支持线程的同步,即任一时刻只有一个线程能写 hashtable,因此也导致了 hashtale在写入时会比较慢。? lin

3、kedllashmap:保存了记录的插入顺序,在用iterator遍历 linkedhashmap时,先得到的记录肯定是先插入的在遍历的时候会比 hashmap慢。key和value均允许为空,非同步的。二、map排序treemap? treemap默认是升序的,如果我们需要改变排序方式,则需要使 用比较器:comparatoro? comparator可以对集合对象或者数组进行排序的比较器接口,实 现该接口的public compared o1 ,to2)法即可实现排序,该方法主要 是根据第一个参数",小于、等于或者大于o2分别返冋负整数、0或者 正整数。如下:public cla

4、ss treemaptest publ ic static void meiin(string args) map<string, string map 二 new trccmap<string, string>(new comparator<string>() public int compare(string objl, string obj2) /降序排序return obj2. compareto(objl););map. put( c , ccccc );map. put,zaaaaa,z);map. put(b", bbbbb); map.

5、put cd", ddddd);set<string> keyset 二 map. keyset();iterator<string> iter 二 kcysct itcrator(); while (iter. hasnext () string key = iter. next ();system, out ptintin(key + : + map. get(key);99999运行结果如下:99999d:dddddc:cccccb:bbbbba:?上面例子是对根据treemap的key值来进行排序的,但是有时我 们需要根据treemap的value来进

6、行排序。对value排序我们就需要借 助于 collections 的 sort (listt> list, comparator/? super t> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有 一个前提条件,那就是所有的元素都必须能够根据所提供的比较器来进 行比较。如下:public class treemaptest public static void mdin(string args) ,zddddd,z) bbbbb) aaaaa )、ccccc )map<string, string> map = new treemap<st

7、ring, string> (); map. put (ct, map. put (b,map. put map. put (c:/这里将 map. entryset ()转换成 listlist<map. entry<string, strin£>> list = new arraylist<map. entry<string, string>>(map. entryset();/然后通过比较器來实现排序collections. sort (list,newcomparator<map entry<string, s

8、tring>>() /升序排序public int compare(entrystring, string> ol, entry<string, string> o2) return ol. gctvaluc () comparcto(o2 gctvaluc (); );for (map. entry<string, string> mapping:list)system, out printin (mapping. getkey () +/z:,z+mapping. getvalue ();?运行结果? a:aaaaab:bbbbbcrcccccd:

9、dddddhashmap?我们都是hashmap的值是没有顺序的,他是按照key的hashcode 来实现的。对于这个无序的hashmap我们要怎么来实现排序呢?参照 treemap的value排序,我们一样的也以实现hashmap的排序。public class hashmaptest publ ic static void m4in(string args) map<string, string> map = new hashmap<string, string>();map. putmap. putmap. putmap. put ("d",c

10、cccc ); aaaaa ); bbbbb); ddddd);list<map. entry<string, string>> list = new arraylist<map. entry<string, string>>(map. entryset();col lections, sort(1ist, newcomparator<map. entry<string, string>>() /升序排序public int compare(entry<string, string> ol,entry<string, string> o2) return ol. getvalue(). compareto(o2. getvalue(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论