在java的众多Map实现中,Map基本上是不能保证顺序的(LinkedHashMap可以保证插入顺序或者访问顺序,TreeMap默认按照key升序但可以自定义Comparator),在开发过程中当数据量不是很大的时候,使用HashMap去统计数据非常方便,但是为了使得输出结果更美观一些,我们需要按某种自定义顺序输出。

下面介绍一种排序Map的方法,使用起来很方便,如下面代码:

     private static class ValueComparator implements Comparator<Map.Entry<String, Integer>> {

         @Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue() - o2.getValue();
}
} public static void test3() {
Map<String, Integer> map = new HashMap<>();
map.put("a", 9);
map.put("b", 8);
map.put("c", 7);
map.put("d", 6);
map.put("e", 5);
map.put("f", 4);
map.put("g", 3);
map.put("h", 2);
map.put("i", 1); List<Map.Entry<String, Integer>> mapList = new ArrayList<>();
mapList.addAll(map.entrySet());
for (Map.Entry<String, Integer> entry : mapList) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println("\n"); ValueComparator valueComparator = new ValueComparator();
Collections.sort(mapList, valueComparator);
for (Map.Entry<String, Integer> entry : mapList) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println("\n");
}

在上面代码中我们先定义一个Comparator,然后将Map转换为List,通过Collections.sort(mapList, valueComparator)方法进行排序输出。

这种写法还可以简化一下,Comparator使用的时候才定义。

         Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareToIgnoreCase(o2.getKey());
}
}; Collections.sort(mapList, comparator);
for (Map.Entry<String, Integer> entry : mapList) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}

使用Java 8之后还可以使用lambda表达式简化一下代码

         Comparator<Map.Entry<String, Integer>> comparator = (o1, o2) -> o1.getKey().compareToIgnoreCase(o2.getKey());

         Collections.sort(mapList, comparator);
for (Map.Entry<String, Integer> entry : mapList) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}

Java.util.Map排序输出的更多相关文章

  1. 接口java.util.Map的四个实现类HashMap Hashtable LinkedHashMap TreeMap

    java中HashMap,LinkedHashMap,TreeMap,HashTable的区别 :java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMa ...

  2. json格式字符串与java.util.Map的互转(借助于jackson.jar)

    package com.test.javaAPI.json; /** * json工具类 * * @author Wei * @time 2016年10月2日 下午4:25:25 */ public ...

  3. Java 8 – Map排序

    前提 Map是Java中最常用的集合类之一,这里整理了关于HashMap的排序 (关于List的排序,请查看Collections.sort()的doc或源码). 将无序的HashMap借助Strea ...

  4. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  5. 错误:java.util.Map is an interface, and JAXB can't handle interfaces.

    问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...

  6. java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.lang.String 转载

    java.sql.SQLException: Invalid parameter object type. Expected 'java.util.Map' but found 'java.lang. ...

  7. java.util.Map.Entry接口

    java.util.Map.Entry接口主要就是在遍历map的时候用到,给你个例子:package test;import java.util.*;import java.util.Map.Entr ...

  8. 在JSTL EL中处理java.util.Map,及嵌套List的情况

    关联的键查找存储在映射中的值. 在方括号中指定键,并将相应的值作为表达式的值返回.例如,表达式 ${map['key']} 返回与 map标识符所引用的 Map 中的 "key" ...

  9. 遍历Map集合:java.util.Map.Entry、KeySet两种方式

    遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...

随机推荐

  1. python坑之input获取字符串

    space = input("set user quotation:").strip() quotation = int(space* 1024 * 1024) print(quo ...

  2. listview相关代码整理

    虽然listview已经慢慢被替代了,  不过还是整理下 , 留作纪念吧 /** * 获取 listview 实际滚动的距离. [ 相对于listview的第一个项目左上角.] * * @return ...

  3. Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)

    来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...

  4. [阿里云部署] Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器

    部署地址:123.56.7.181 Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器 这个标题就比之前的"ECS服务器配置Web环境的全过程及参考资料&qu ...

  5. 【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)

    微服务体系,有效解决项目庞大.互相依赖的问题.目前SpringCloud体系有强大的一整套针对微服务的解决方案.本文中,重点对微服务体系中的服务发现注册中心进行详细说明.本篇中的注册中心,采用Netf ...

  6. webuploader 实现图片批量上传

    1.导入资源 2.JSP代码 <div class="page-container"> <div class="row cl"> < ...

  7. Shell编程基础篇

    1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, Linux/UNIX系统的底层及基础应用软件的核心大都涉及Shell脚 ...

  8. 使用superMap实现点标注和区域着色

    1.定义html文件,引入superMap的js和theme文件: <script src='${_ctxPath }/statics/js/superMap/SuperMap.Include. ...

  9. 16进制到byte转换

    我们经常会看到这样的语法 (byte) 0xAD 0xAD实际是个16进制,转换成二进制为:10101101,转换成10进制是:173,它是个正数 10101101只是int的简写,int由4个byt ...

  10. 基于Accord.Audio和百度语言识别

    ---恢复内容开始--- 目标需求 使用录音形式,模拟微信语音聊天.按住录音,松开发送语音,并完成语音识别. ps:百度的语言识别有60秒长度限制,需要自己做好控制. 实现方案 采用C# winfor ...