使用pinyin4j将TreeMap按照key的汉字拼音排序
来源:互联网 发布:世界秩序 知乎 编辑:程序博客网 时间:2024/06/08 00:28
今天由于工作需要,需要对Map中的key值进行排序,原来代码中用的是HashMap,由于HashMap构造器不能接收Comparator参数,所以改用TreeMap,对Treemap<String,String>的key值的汉字进行拼音排序,就用到了pinyin4j的包,如代码
Map<String, List<Camera>> ownerCamerasMap = new TreeMap<String, List<Camera>>(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {if (o1 == null || o2 == null || o1.equals("") || o2.equals("") || o1.equals(o2))return 0;//实际需要,要将key值为 '本地摄像头' 放在第一位,'常用摄像头'放在第二位if(o1.equals("本地摄像头"))return -1;if(o2.equals("本地摄像头"))return 1;if(o1.equals("常用摄像头") && !o2.equals("本地摄像头"))return -1;if(o2.equals("常用摄像头") && !o1.equals("本地摄像头"))return 1;//以汉字少的为基准for(int i = 0; i < Math.min(o1.length(), o2.length()); i++){char c1 = o1.charAt(i); char c2 = o2.charAt(i); int result = concatPinyinStringArray( PinyinHelper.toHanyuPinyinStringArray(c1)).compareTo( concatPinyinStringArray(PinyinHelper .toHanyuPinyinStringArray(c2))); //如果比较结果相同,就比较下一个汉字 if(result == 0){ continue; } return result; }//循环执行完仍不能得到结果,汉字拼音首字母相同,则直接比较字符串 return o1.compareTo(o2);}//调用pinyin4j工具类private String concatPinyinStringArray(String[] pinyinArray) { StringBuffer pinyinSbf = new StringBuffer(); if ((pinyinArray != null) && (pinyinArray.length > 0)) { for (int i = 0; i < pinyinArray.length; i++) { pinyinSbf.append(pinyinArray[i]); } } return pinyinSbf.toString(); } });
1 0
- 使用pinyin4j将TreeMap按照key的汉字拼音排序
- 使用pinyin4j将汉字转换成拼音
- TreeMap按照key排序
- TreeMap按照key排序
- TreeMap按照key排序
- TreeMap按照key排序
- TreeMap按照key排序
- C++使用汉字排序(按照拼音排序)
- 使用PinYin4j.jar将汉字转换为拼音使用实例
- 按照汉字拼音排序
- 汉字按照拼音排序
- 汉字按照拼音排序
- 使用PinYin4j.jar将汉字转换为拼音
- 使用PinYin4j.jar将汉字转换为拼音
- 使用PinYin4j.jar将汉字转换为拼音
- 使用PinYin4j.jar将汉字转换为拼音
- java使用PinYin4j将汉字转换为拼音
- 使用PinYin4j.jar将汉字转换为拼音
- mysql遇到的报错
- win8配置JDK1.8环境变量
- 关于C#中get和set
- 简单方法解决设置IBOutlet变量时中出现的“Could not insert new outlet connection”
- 详解基于jsp的文件的上传下载
- 使用pinyin4j将TreeMap按照key的汉字拼音排序
- java web 将http 强转为https
- >Error Occured in WS-AppServe
- poj1094
- 一张图理解mapreduce的shuffle
- windows 远程桌面 Ubuntu14.04 配置记录
- photoshop不透明度和填充度原理
- hdu4463Outlets
- Android5.1中surface和CpuConsumer下生产者和消费者间的处理框架简述