Java集合框架学习(九) TreeMap详解
TreeMap介绍
TreeMap 类实现了Map接口,和HashMap类类似。
TreeMap是一个基于Red-Black tree的可导航map的实现。 它基于key的自然顺序排序。
TreeMap和HashMap在排序上。
TreeMap是非线程同步的。
类定义
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, Serializable
例子介绍
package com.dylan.collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class TreeMapExample {
public static void main(String args[]) {
/* This is how to declare TreeMap */
TreeMap<Integer, String> tmap =
new TreeMap<>();
/*Adding elements to TreeMap*/
tmap.put(1, "Data1");
tmap.put(23, "Data2");
tmap.put(70, "Data3");
tmap.put(4, "Data4");
tmap.put(2, "Data5");
/* Display content using Iterator*/
Set set = tmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
}
}
如何让TreeMap按value排序
package com.dylan.collection;
import java.util.*;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class TreeMapSortByValue {
//Method for sorting the TreeMap based on values
public static <K, V extends Comparable<V>> Map<K, V>
sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator =
new Comparator<K>() {
public int compare(K k1, K k2) {
int compare =
map.get(k1).compareTo(map.get(k2));
if (compare == 0)
return 1;
else
return compare;
}
};
Map<K, V> sortedByValues =
new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
public static void main(String args[]) {
TreeMap<String, String> treemap = new TreeMap<String, String>();
// Put elements to the map
treemap.put("Key1", "Jack");
treemap.put("Key2", "Rick");
treemap.put("Key3", "Kate");
treemap.put("Key4", "Tom");
treemap.put("Key5", "Steve");
// Calling the method sortByvalues
Map sortedMap = sortByValues(treemap);
// Get a set of the entries on the sorted map
Set set = sortedMap.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}
如何让TreeMap按Key倒序迭代
package com.dylan.collection;
import java.util.*;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class TreeMapOrderByKeyDesc {
public static void main(String args[]) {
Map<String, String> treemap =
new TreeMap<String, String>(Collections.reverseOrder());
// Put elements to the map
treemap.put("Key1", "Jack");
treemap.put("Key2", "Rick");
treemap.put("Key3", "Kate");
treemap.put("Key4", "Tom");
treemap.put("Key5", "Steve");
Set set = treemap.entrySet();
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}
如何获取TreeMap的子集
package com.dylan.collection;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class TreeMapSubMap {
public static void main(String args[]) {
// Create a TreeMap
TreeMap<String, String> treemap =
new TreeMap<String, String>();
// Put elements to the map
treemap.put("Key1", "Jack");
treemap.put("Key2", "Rick");
treemap.put("Key3", "Kate");
treemap.put("Key4", "Tom");
treemap.put("Key5", "Steve");
treemap.put("Key6", "Ram");
// Displaying TreeMap elements
System.out.println("TreeMap Contains : " + treemap);
// Getting the sub map
/* public SortedMap<K,V> subMap(K fromKey,K toKey): Returns
* a view of the portion of this map whose keys range from
* fromKey, inclusive, to toKey, exclusive.
* (If fromKey and toKey are equal, the returned map is empty.)
* The returned map is backed by this map, so changes in the
* returned map are reflected in this map, and vice-versa.
* The returned map supports all optional map operations that
* this map supports.
*/
SortedMap<String, String> sortedMap = treemap.subMap("Key2","Key5");
System.out.println("SortedMap Contains : " + sortedMap);
// Removing an element from Sub Map
sortedMap.remove("Key4");
/* Displaying elements of original TreeMap after
* removing an element from the Sub Map. Since Sub Map is
* backed up by original Map, the element should be removed
* from this TreeMap too.
*/
System.out.println("TreeMap Contains : " + treemap);
}
}
Java集合框架学习(九) TreeMap详解的更多相关文章
- Java集合框架学习(一)List
先附一张Java集合框架图. 从上面的集合框架图可以看到,Java集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Coll ...
- Java集合框架学习
集合框架 集合框架的目标 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现必须是高效的. 该框架允许 不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的扩展和适应必 ...
- Java 集合Collection与List的详解
1.什么是集合 存储对象的容器,面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式. 集合的出现就是为了持有对象.集合中可以存储任意类型的 ...
- 【java基础 11】java集合框架学习
导读:本篇博客主要是从整体上了解java的集合框架,然后主要介绍几个自己在项目中用到的结构,比如说:hashtable.hashmap.hashset.arraylist等! 一.宏观预览 从宏观上看 ...
- java集合的方法及使用详解
一.java集合的分类及相互之间的关系 Collection接口:向下提供了List和Set两个子接口 |------List接口:存储有序的,存储元素可以重复 |------ArrayList(主要 ...
- java提高篇(九)-----详解匿名内部类
在java提高篇-----详解内部类中对匿名内部类做了一个简单的介绍,但是内部类还存在很多其他细节问题,所以就衍生出这篇博客.在这篇博客中你可以了解到匿名内部类的使用.匿名内部类要注意的事项.如何初始 ...
- Java集合框架学习笔记
集合类的由来:对象用于封装特有数据,对象多了需要存储,如果对象的长度不确定,就使用集合存储. 集合特点1.用于存储对象的容器.2.集合的长度可变.3.集合中不可以存储基本类型 集合容器因为内部的数据结 ...
- Java后端框架之Spring Boot详解,文末有Java分布式实战项目视频可取
在 Java 后端框架繁荣的今天,Spring 框架无疑是最最火热,也是必不可少的开源框架,更是稳坐 Java 后端框架的龙头老大. 用过 Spring 框架的都知道 Spring 能流行是因为它的两 ...
- Java集合框架学习总结
转自:http://www.cnblogs.com/oubo/archive/2012/01/07/2394639.html Oubo的博客 以下介绍经常使用的集合类,这里不介绍集合类的使用方法,只介 ...
- 《Java Spring框架》SpringXML配置详解
Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...
随机推荐
- ONVIF网络摄像头(IPC)客户端开发—ONVIF介绍
1.前言: 网上已经有很多关于ONVIF开发的资料,这里概括介绍一下ONVIF协议以及介绍一下我自己在开发ONVIF网络摄像头的一些流程和经验,做个开发记录和经验总结,以备将来查看,也可供他人参考 ...
- 监控服务器所有磁盘的inode使用情况
监控服务器所有磁盘的inode使用情况 背景 因为前期数据库开启了审计 但是如果是 DB模式的话 $aud 表的冲突和使用太多了 所以专家建议将审计表放到OS 因为数据库的访问量特别高. 审计的信息又 ...
- [转帖]Shell三剑客之awk
目录 awk简述 awk的工作过程 awk的工作原理 awk的基本格式及其内置变量 getline 文本内容匹配过滤打印 对字段进行处理打印 条件判断打印 awk的三元表达式与精准筛选用法 awk的精 ...
- Docker镜像精简方法之二 COPY vs ADD 与镜像层
Docker镜像精简方法之二 COPY vs ADD 与镜像层 摘要 昨天只是讲了一下大体的思路. 但是没有实操. 今天想着修改一下默认的打包镜像的命令,验证一下效果 原始命令 FROM adopto ...
- [转帖]重置 VCSA 6.7 root密码和SSO密码
问题描述 1.用root用户登录 VMware vCenter Server Appliance虚拟机失败,无法登录 2.vCenter Server Appliance 6.7 U1的root帐户错 ...
- 人工智能GPT科普知识的简单总结
人工智能GPT相关知识的简单总结 背景 工作已经很久, 工作十几年来有过好多波新的技术浪潮. 但是每次都离技术前沿比较远. 最近发现只低头拉车是一个没有前途的行为. 人生很短, 选择很重要, 不仅要低 ...
- 【转帖】基于官方rpm包方式安装Oracle19c
https://blog.whsir.com/post-5489.html 本文基于Centos7.x环境,通过官方提供的rpm包来安装19c 1.下载Oracle19c安装包 https://w ...
- [转帖]jvm学习三-MAT内存分析工具的使用
目录 1 模拟内存溢出程序 1.1 jvm配置 1.2 测试代码 2 MAT工具进行内存分析 2.1 大纲介绍 2.2 Histogram视图介绍 2.3 Leak Suspects视图介绍 2.4 ...
- BPF的简单学习
BPF的简单学习 前言 本来规划过年期间学习一下bpf相关的内容 但是因为自己没有坚持学习,所以到最后一天才开始整理. 本来想深入学习一下相关内容,但是已经感觉已经无法完成. 最近大半年进行了很多性能 ...
- R2M分布式锁原理及实践
作者:京东科技 张石磊 1 案例引入 名词简介: 资源:可以理解为一条内容,或者图+文字+链接的载体. 档位ID: 资源的分类组,资源必须归属于档位. 问题描述:当同一个档位下2条资源同时审批通过时, ...