HashMap与TreeMap按照key和value排序
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排序的更多相关文章
- [Java] HashMap、TreeMap、Hashtable排序
Java中对Map(HashMap,TreeMap,Hashtable等)的排序时间 首先简单说一下他们之间的区别: HashMap: 最常用的Map,它根据键的HashCode 值存储数据,根据键可 ...
- TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置
package com.swift; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; ...
- 论HashMap、Hashtable、TreeMap、LinkedHashMap的内部排序
参考文章 论HashMap.Hashtable.TreeMap.LinkedHashMap的内部排序
- HashMap与TreeMap源码分析
1. 引言 在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...
- Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |
Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...
- Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。
Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...
- 【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别
转自:http://blog.csdn.net/paincupid/article/details/47746341 一.HashMap和TreeMap区别 1.HashMap是基于散列表实现的,时间 ...
- HashMap,LinkedHashMap,TreeMap的区别
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复. Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很 ...
- HashMap vs TreeMap vs Hashtable vs LinkedHashMap
Map是一个重要的数据结构,本篇文章将介绍如何使用不同的Map,如HashMap,TreeMap,HashTable和LinkedHashMap. Map概览 Java中有四种常见的Map实现,Has ...
随机推荐
- nRF52832定时器
1概述 定时器能够被配置为两种模式:定时模式和计数模式,nrf52832有五个定时器,timer0--timer4 . 2常用得函数 函数功能:初始化定时器 ret_code_t nrf_drv_ti ...
- Git安装与使用
转载自:https://www.cnblogs.com/smuxiaolei/p/7484678.html git 提交 全部文件 git add . git add xx命令可以将xx文件添加到暂 ...
- C#复习题(概念) --C#学习笔记
第一章 1.公共语言架构(CLI)由哪几部分组成? (1)通用类型系统:定义了一套类型系统的框架,规定了数据类型的声明.使用和管理方法. (2)公共语言规范:一组语言规则的集合 (3)通用中间语言:一 ...
- sgu 203 Hyperhuffman
题意:给出字符出现的次数,问替换成哈夫曼编码后的文本长度. 实际上观察发现就等于树的所有节点的和.用nlogn超时.用O(n),用两个队列,一个放原始数组,一个放新生成的节点. #include &l ...
- Android 如何更改一个 imageview的颜色
xml中可以使用tint属性 tips:这里的图片必须是png格式的文件才行,不然你得到的就是一个被纯色覆盖的图 java中 使用 imageView.setColorFilter(Color.WHI ...
- Docker 镜像上传到docker hub仓库
1 先创建docker hub 帐号 ,并创建仓库 https://hub.docker.com/ 首先你需要一个docker hub 帐号,记住username,password,email .后面 ...
- First Bad Version leetcode java
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 『计算机视觉』Mask-RCNN_推断网络其四:FPN和ROIAlign的耦合
一.模块概述 上节的最后,我们进行了如下操作获取了有限的proposal, # [IMAGES_PER_GPU, num_rois, (y1, x1, y2, x2)] # IMAGES_PER_GP ...
- Shiro中Realm
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色 ...
- iptables -F 与 -X 区别
test: 1.iptables 初始化 2.iptables -X (第一次) 错误原因是自定义链表(test)不为空 3.iptables -F 4.iptables -X ok,实验结束 实验报 ...