package com.sort;

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; /**
* Map的排序
*
* @author root
*
*/
public class MapSort {
/**
* TreeMap按照key进行排序
*/
public static void TreeMapSortByKey() {
Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// 降序排列
return o2.compareTo(o1);
}
});
map.put("c", "ccccc");
map.put("a", "aaaaa");
map.put("b", "bbbbb");
map.put("d", "ddddd");
for (String key : map.keySet()) {
System.err.println("key:" + key + " value:" + map.get(key));
}
} /**
* TreeMap按照value排序
*/
public static void TreeMapSortByValue() {
Map<String, String> map = new TreeMap<String, String>();
map.put("d", "ccccc");
map.put("b", "bbbbb");
map.put("a", "eeeee");
map.put("c", "ddddd");
// 将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>>() {
@Override
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());
}
} /**
* Map按照Key排序
*/
public static void MapSortByKey() {
Map<String, String> map = new HashMap<String, String>();
map.put("d", "ccccc");
map.put("b", "bbbbb");
map.put("a", "eeeee");
map.put("c", "ddddd");
// 将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>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
// 升序排序
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, String> mapping : list) {
System.out.println(mapping.getKey() + ":" + mapping.getValue());
}
} /**
* Map按照Value排序
*/
public static void MapSortByValue() {
Map<String, String> map = new HashMap<String, String>();
map.put("d", "ccccc");
map.put("b", "bbbbb");
map.put("a", "eeeee");
map.put("c", "ddddd");
// 将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>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
// 降序排序
return o2.getValue().compareTo(o1.getValue());
}
});
for (Map.Entry<String, String> mapping : list) {
System.out.println("key:"+mapping.getKey() + " value:" + mapping.getValue());
}
} public static void main(String[] args) {
// MapSort.TreeMapSortByKey();
// MapSort.TreeMapSortByValue();
MapSort.MapSortByKey();
MapSort.MapSortByValue();
}
}

HashMap与TreeMap按照key和value排序的更多相关文章

  1. [Java] HashMap、TreeMap、Hashtable排序

    Java中对Map(HashMap,TreeMap,Hashtable等)的排序时间 首先简单说一下他们之间的区别: HashMap: 最常用的Map,它根据键的HashCode 值存储数据,根据键可 ...

  2. TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置

    package com.swift; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; ...

  3. 论HashMap、Hashtable、TreeMap、LinkedHashMap的内部排序

    参考文章 论HashMap.Hashtable.TreeMap.LinkedHashMap的内部排序

  4. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  5. Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |

    Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...

  6. Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。

    Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...

  7. 【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别

    转自:http://blog.csdn.net/paincupid/article/details/47746341 一.HashMap和TreeMap区别 1.HashMap是基于散列表实现的,时间 ...

  8. HashMap,LinkedHashMap,TreeMap的区别

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复. Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很 ...

  9. HashMap vs TreeMap vs Hashtable vs LinkedHashMap

    Map是一个重要的数据结构,本篇文章将介绍如何使用不同的Map,如HashMap,TreeMap,HashTable和LinkedHashMap. Map概览 Java中有四种常见的Map实现,Has ...

随机推荐

  1. P359 usestock2.cpp

    IDE Qt Creator 4.0.3 stock.h #ifndef STOCK_H #define STOCK_H #include <string> class Stock //类 ...

  2. 在Windows下解决git ERROR: Permission to XXX.git denied to user

    这种情况一般都是由于登陆了不同的git仓库在本地记录了凭证导致的,比如登陆了两个不同的github账号. 1.控制面板 2.删除凭证再重新提交将会重新输入用户名和密码 以上.

  3. ubuntu下安装anaconda

    1.  到官网http://continuum.io/downloads下载anaconda. 选择linux64-bit-python2.7 2.  安装anaconda,在终端输入:cd ~/Do ...

  4. Python全栈开发-Day5-常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 pyyaml处理 configparser hashlib re正则 ...

  5. python+selenium2(一)

    一.安装python (1)在官网下载python的安装包,这里使用的是python3.5.安装一路下一步,安装路径我的是D:\Python35. (2)在计算机的path变量中添加D:\Python ...

  6. 最大的矩形面积 Maximal Rectangle

    2018-09-15 10:23:44 一.Largest Rectangle in Histogram 在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题. 问题描述: 问题求解: 解法一 ...

  7. php中文件操作常用函数有哪些

    php中文件操作常用函数有哪些 一.总结 一句话总结:读写文件函数 判断文件或者目录是否存在函数 创建目录函数 file_exists() mkdir() file_get_content() fil ...

  8. mysql索引优化比普通查询速度快多少

    mysql索引优化比普通查询速度快多少 一.总结 一句话总结:普通查询全表查询,速度较慢,索引优化的话拿空间换时间,一针见血,所以速度要快很多. 索引优化快很多 空间换时间 1.软件层面优化数据库查询 ...

  9. change color1

    private void Form1_Load(object sender, EventArgs e)         {             string str = "server= ...

  10. Lab 3-2

    Analyze the malware found in the file Lab03-02.dll using basic dynamic analysis tools. Questions and ...