当前位置: 首页 > news >正文

深圳微信网站建设报价长沙网络营销外包哪家好

深圳微信网站建设报价,长沙网络营销外包哪家好,免费自己做网站,网页设计图片左右滚动Map排序的方式有很多种&#xff0c;这里记录下自己总结的两种比较常用的方式&#xff1a;按键排序(sort by key)&#xff0c; 按值排序(sort by value)。 1、按键排序 jdk内置的java.util包下的TreeMap<K,V>既可满足此类需求&#xff0c;向其构造方法TreeMap(Comparato…

Map排序的方式有很多种,这里记录下自己总结的两种比较常用的方式:按键排序(sort by key), 按值排序(sort by value)。

1、按键排序

jdk内置的java.util包下的TreeMap<K,V>既可满足此类需求,向其构造方法TreeMap(Comparator< super K> comparator) 传入我们自定义的比较器即可实现按键排序。

实现代码

public class MapSortDemo {public static void main(String[] args) {Map<String, String> map = new TreeMap<String, String>();map.put("KFC", "kfc");map.put("WNBA", "wnba");map.put("NBA", "nba");map.put("CBA", "cba");Map<String, String> resultMap = sortMapByKey(map);	//按Key进行排序for (Map.Entry<String, String> entry : resultMap.entrySet()) {System.out.println(entry.getKey() + " " + entry.getValue());}}/*** 使用 Map按key进行排序* @param map* @return*/public static Map<String, String> sortMapByKey(Map<String, String> map) {if (map == null || map.isEmpty()) {return null;}Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());sortMap.putAll(map);return sortMap;}
}

比较器类

class MapKeyComparator implements Comparator<String>{@Overridepublic int compare(String str1, String str2) {return str1.compareTo(str2);}
}

2、按值排序

按值排序就相对麻烦些了,貌似没有直接可用的数据结构能处理类似需求,需要我们自己转换一下。
Map本身按值排序是很有意义的,很多场合下都会遇到类似需求,可以认为其值是定义的某种规则或者权重。

原理:将待排序Map中的所有元素置于一个列表中,接着使用Collections的一个静态方法 sort(List list, Comparator< super T> c)
来排序列表,同样是用比较器定义比较规则。排序后的列表中的元素再依次装入Map,为了肯定的保证Map中元素与排序后的List中的元素的顺序一致,使用了LinkedHashMap数据类型。

实现代码

public class MapSortDemo {public static void main(String[] args) {Map<String, String> map = new TreeMap<String, String>();map.put("KFC", "kfc");map.put("WNBA", "wnba");map.put("NBA", "nba");map.put("CBA", "cba");Map<String, String> resultMap = sortMapByKey(map);	//按Key进行排序
//		Map<String, String> resultMap = sortMapByValue(map); //按Value进行排序for (Map.Entry<String, String> entry : resultMap.entrySet()) {System.out.println(entry.getKey() + " " + entry.getValue());}}/*** 使用 Map按value进行排序* @param map* @return*/public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {if (oriMap == null || oriMap.isEmpty()) {return null;}Map<String, String> sortedMap = new LinkedHashMap<String, String>();List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());Collections.sort(entryList, new MapValueComparator());Iterator<Map.Entry<String, String>> iter = entryList.iterator();Map.Entry<String, String> tmpEntry = null;while (iter.hasNext()) {tmpEntry = iter.next();sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());}return sortedMap;}
}

比较器类

class MapValueComparator implements Comparator<Map.Entry<String, String>> {@Overridepublic int compare(Entry<String, String> me1, Entry<String, String> me2) {return me1.getValue().compareTo(me2.getValue());}
}
http://www.hkea.cn/news/342184/

相关文章:

  • 国际外贸网站电子商务
  • 南充做网站 www.xinbay.com全国免费发布广告信息
  • 备案 个人网站软件开发培训中心
  • 江苏网站建设网络推广关键词批量调词 软件
  • 东莞企业网站建设价格怎么在百度发布免费广告
  • 网站后台地址一般是在线seo优化工具
  • 海曙区住房和建设局网站备案域名
  • 网站建设硬件环境志鸿优化设计答案
  • 网页游戏网址推荐宁波网站推广网站优化
  • 福建就福建省住房与城乡建设厅网站高端网站建设企业
  • 网站如何做seo规划app怎么开发出来的
  • 吴江住房和城乡建设局官方网站产品软文是什么
  • 公司网站制作设谷歌seo是什么职业
  • 北京品牌高端网站建设公司燕郊今日头条
  • 网站制作公司徐州宁波网站seo哪家好
  • 做网站基本费用大概需要多少全媒体运营师报考官网在哪里
  • 网站建设款属于什么科目营业推广策划
  • 建设网站查证书网络广告有哪些形式
  • 分布式网站开发网络销售平台排名
  • 网站建设模板购买品牌seo培训
  • 深圳网站建设 cms网站推广交换链接
  • 标准物质网站建设5118站长工具箱
  • 做一个能注册用户的网站网络推广费用大概价格
  • 网站建设评价东莞谷歌推广
  • php网站后台进不去百度推广入口官网
  • 个人网站一键生成免费推广网站有哪些
  • 厦门做网站设计电商seo优化
  • wordpress视频点播seo技术是干什么的
  • 网站推广是怎么做的网络营销专业如何
  • 平面设计线上兼职上海网站seo