【JDK1.8】JDK1.8集合源码阅读——TreeMap(一)
一、前言
在前面两篇随笔中,我们提到过,当HashMap的桶过大的时候,会自动将链表转化成红黑树结构,当时一笔带过,因为我们将留在本章中,针对TreeMap进行详细的了解。
二、TreeMap的继承关系
下面先让我们来看一下TreeMap的继承关系,对它有一个大致的了解:

可以看到,除了在之前HashMap里常见的继承类和接口以外,TreeMap实现了NavigableMap接口,而NavigableMap继承自SortedMap,由名字可以看出,只是一个用来实现排序的接口。而这也是为什么TreeMap能够实现排序的原因。由于篇幅关系,将TreeMap的源码解析分为三部分,本章将对接口NavigableMap以及SortedMap进行解析。
三、SortedMap接口源码解析
3.1 SortedMap接口
public interface SortedMap<K,V> extends Map<K,V> {
//返回用于对键的进行排序的比较器,如果此映射使用其键的自然排序,则为null
Comparator<? super K> comparator();
//返回从fromKey(包括)到toKey(不包括)之间的map
SortedMap<K,V> subMap(K fromKey, K toKey);
//返回小于toKey的map
SortedMap<K,V> headMap(K toKey);
//返回大于或等于fromKey的map
SortedMap<K,V> tailMap(K fromKey);
//返回map中第一个(最低)键
K firstKey();
//返回map中最后一个(最高)键
K lastKey();
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();
}
SortedMap的接口比较简单,没有很特别的地方,唯一比较特别的就是返回Comparator这个接口,可以设想实现排序功能的秘密或许就藏在此处。下面让我们来看一下Comparator和Comparable接口,两者之间有点关联,可以理解为Comparable自带了比较功能,而Comparator是赋予没有比较能力的对象一种比较能力。举个简单例子:面对一道计算题,小明天生口算能力很强,看一眼就能算出来答案。而小李没有这种能力,需要借助计算器才能得出答案。
3.2 Comparable接口
先让我们看下它的代码:
public interface Comparable<T> {
//如果小于o,返回负数;等于o,返回0;大于o返回正数。
public int compareTo(T o);
}
对,就是这么简单,里面传入一个泛型T的对象o,对o进行比较。如果小于o,返回负数;等于o,返回0;大于o返回正数。
我们熟悉的很多对象如String,Integer,Double等都实现了这个接口。可以来看一下简单的例子:
public class Item implements Comparable<Item> {
private String name;
private int price;
public Item(String name, int price) {
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
@Override
public int compareTo(Item o) {
if (this.name.compareTo(o.name) < 0) {
return -1;
} else if (this.name.compareTo(o.name) > 0) {
return 1;
} else {
return 0;
}
}
public static void main(String[] args) {
List<Item> items = Arrays.asList(new Item("banana", 200), new Item("apple", 400));
System.out.println("before:" + items);
Collections.sort(items);
System.out.println("after:" + items);
}
}
上述main函数的输出:
before:[Item{name='banana', price=200}, Item{name='apple', price=400}]
after:[Item{name='apple', price=400}, Item{name='banana', price=200}]
上面的例子中,我们自己实现了Comparable接口,比较了Item的name属性,然后通过Collections.sort对它进行了排序(值得注意的是:没有实现Comparable接口的对象不能使用该方法)。但是,如果我不想用name属性对它进行排序,想对price进行排序呢,或者先对name排序,相同的话在对price进行排序呢,用这个不就没法实现了吗。这就需要提到了下面的Comparator接口
3.3 Comparator接口
照例先来看一下代码:
@FunctionalInterface
public interface Comparator<T> {
// 核心方法,用来比较两个对象,如果o1小于o2,返回负数;等于o2,返回0;大于o2返回正数
int compare(T o1, T o2);
// 好像很少用到,一般都用对象自带的equals
boolean equals(Object obj);
/**-----------下面的都是JDK1.8新增的接口,挑几个放进去----------*/
//返回反向排序比较器
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
//根据名字知道,先进行compare比较后,再进行一次比较
default Comparator<T> thenComparing(Comparator<? super T> other) {
Objects.requireNonNull(other);
return (Comparator<T> & Serializable) (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
//对int类型的key进行比较
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
}
//返回正常顺序的比较器
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
}
}
一起来看一下如何使用,先来看一下JDK1.8以前的用法:
public class SimpleComparator implements Comparator<Item> {
@Override
public int compare(Item o1, Item o2) {
return o1.price - o2.price;
}
public static void main(String[] args) {
List<Item> items = Arrays.asList(new Item("banana", 200), new Item("apple", 400), new Item("Orange", 100));
Collections.sort(items, new SimpleComparator());
System.out.println(items);
}
}
上述main函数的输出是:
[Item{name='Orange', price=100}, Item{name='banana', price=200}, Item{name='apple', price=400}]
JDK1.8以前的用法要自己手动实现Comparator接口,然后调用Collections.sort(),传入实现类来完成排序,非常麻烦,而JDK1.8则相对来说简单了很多:
public static void main(String[] args) {
List<Item> items = Arrays.asList(new Item("banana", 200), new Item("apple", 400), new Item("Orange", 100));
Collections.sort(items, (Item a, Item b) -> a.price - b.price);
System.out.println(items);
}
甚至,我们可以不使用Collections.sort:
public static void main(String[] args) {
List<Item> items = Arrays.asList(new Item("banana", 100), new Item("Orange", 100), new Item("apple", 400), new Item("Orange", 50));
items.sort((Item a, Item b) -> a.price - b.price);
System.out.println(items);
//使用上面的thenComparing
items.sort(Comparator.comparing(Item::getName).thenComparing(Comparator.comparingInt(Item::getPrice)));
System.out.println("after using thenComparing: " + items);
}
上述main函数的输出:
[Item{name='orange', price=50}, Item{name='banana', price=100}, Item{name='orange', price=100}, Item{name='apple', price=400}]
after using thenComparing: [Item{name='apple', price=400}, Item{name='banana', price=100}, Item{name='orange', price=50}, Item{name='orange', price=100}]
四、NavigableMap接口源码解析
public interface NavigableMap<K,V> extends SortedMap<K,V> {
//返回键小于且最接近Key(不包含等于)的键值对,没有返回null
Map.Entry<K,V> lowerEntry(K key);
//返回小于且最接近(不包含等于)Key的键,没有返回null
K lowerKey(K key);
//返回键小于且最接近(包含等于)Key的键值对,没有返回null
Map.Entry<K,V> floorEntry(K key);
//返回小于且最接近(包含等于)Key的键,没有返回null
K floorKey(K key);
//返回大于且最接近(包含等于)给定key的键值对,没有返回null
Map.Entry<K,V> ceilingEntry(K key);
//同上
K ceilingKey(K key);
//返回大于且最接近(不包含等于)给定key的键值对
Map.Entry<K,V> higherEntry(K key);
//同上
K higherKey(K key);
//返回第一个Entry
Map.Entry<K,V> firstEntry();
//返回最后一个Entry
Map.Entry<K,V> lastEntry();
//移除并返回第一个Entry
Map.Entry<K,V> pollFirstEntry();
//同上
Map.Entry<K,V> pollLastEntry();
//返回map中包含的映射关系的逆序视图
NavigableMap<K,V> descendingMap();
//返回map中包含的键的NavigableSet视图。 set的迭代器按key的升序
NavigableSet<K> navigableKeySet();
//逆序
NavigableSet<K> descendingKeySet();
//根据fromKey和toKey来返回子map,两个boolean参数用于是否包含该key
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive);
//返回小于(或等于,根据inclusive)toKey的map
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
//返回大于(或等于,根据inclusive)fromKey的map
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
SortedMap<K,V> subMap(K fromKey, K toKey);
SortedMap<K,V> headMap(K toKey);
SortedMap<K,V> tailMap(K fromKey);
}
注意:上述返回的map与原map是相互影响的。
五、总结
本章分析了TreeMap的继承关系,给后面分析TreeMap作为铺垫。SortedMap和NavigableMap的接口中,包含了大量的返回Map的方法,这也是作为排序Map的一大特点吧。最后谢谢各位园友观看,与大家共同进步!
【JDK1.8】JDK1.8集合源码阅读——TreeMap(一)的更多相关文章
- 【JDK1.8】JDK1.8集合源码阅读——TreeMap(二)
一.前言 在前一篇博客中,我们对TreeMap的继承关系进行了分析,在这一篇里,我们将分析TreeMap的数据结构,深入理解它的排序能力是如何实现的.这一节要有一定的数据结构基础,在阅读下面的之前,推 ...
- 【JDK1.8】Java 8源码阅读汇总
一.前言 万丈高楼平地起,相信要想学好java,仅仅掌握基础的语法是远远不够的,从今天起,笔者将和园友们一起阅读jdk1.8的源码,并将阅读重点放在常见的诸如collection集合以及concu ...
- 【JDK1.8】JDK1.8集合源码阅读——IdentityHashMap
一.前言 今天我们来看一下本次集合源码阅读里的最后一个Map--IdentityHashMap.这个Map之所以放在最后是因为它用到的情况最少,也相较于其他的map来说比较特殊.就笔者来说,到目前为止 ...
- java1.7集合源码阅读: Stack
Stack类也是List接口的一种实现,也是一个有着非常长历史的实现,从jdk1.0开始就有了这个实现. Stack是一种基于后进先出队列的实现(last-in-first-out (LIFO)),实 ...
- java1.7集合源码阅读: Vector
Vector是List接口的另一实现,有非常长的历史了,从jdk1.0开始就有Vector了,先于ArrayList出现,与ArrayList的最大区别是:Vector 是线程安全的,简单浏览一下Ve ...
- 【JDK1.8】JDK1.8集合源码阅读——总章
一.前言 今天开始阅读jdk1.8的集合部分,平时在写项目的时候,用到的最多的部分可能就是Java的集合框架,通过阅读集合框架源码,了解其内部的数据结构实现,能够深入理解各个集合的性能特性,并且能够帮 ...
- 【JDK1.8】JDK1.8集合源码阅读——HashMap
一.前言 笔者之前看过一篇关于jdk1.8的HashMap源码分析,作者对里面的解读很到位,将代码里关键的地方都说了一遍,值得推荐.笔者也会顺着他的顺序来阅读一遍,除了基础的方法外,添加了其他补充内容 ...
- 【JDK1.8】JDK1.8集合源码阅读——ArrayList
一.前言 在前面几篇,我们已经学习了常见了Map,下面开始阅读实现Collection接口的常见的实现类.在有了之前源码的铺垫之后,我们后面的阅读之路将会变得简单很多,因为很多Collection的结 ...
- 【JDK1.8】JDK1.8集合源码阅读——LinkedList
一.前言 这次我们来看一下常见的List中的第二个--LinkedList,在前面分析ArrayList的时候,我们提到,LinkedList是链表的结构,其实它跟我们在分析map的时候讲到的Link ...
随机推荐
- pseudocode of zigzag conversion
1.Title : The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- 面试题:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- python初步(附学习思维导图)
python,原意为蟒蛇,至于它的发展史,度娘应该比我讲述的更为专业/偷笑.这里我们要梳理的是整个学习的脉络,当然,今后的随笔也会从基础部分说起,希望能给进门python的小伙伴一些建议. 一.环境的 ...
- UVW源码漫谈(四)
十一假期后就有点懒散,好长时间都没想起来写东西了.另外最近在打LOL的S赛.接触LOL时间不长,虽然平时玩的比较少,水平也相当菜,但是像这种大型的赛事有时间还是不会错过的.主要能够感受到选手们对竞技的 ...
- RabbitMQ 笔记-RPC
RabbitMQ中实现RPC的机制是: 客户端发送请求(消息)时,在消息的属性(MessageProperties,在AMQP协议中定义了14中properties,这些属性会随着消息一起发送)中设置 ...
- [ACdream] 女神教你字符串——三个气球
Problem Description 女神邀请众ACdream开联欢会,显然作为ACM的佼佼者,气球是不能少的~.女神准备了三种颜色的气球,红色,黄色,绿色(交通信号灯?) 有气球还不能满足女神,女 ...
- LeetCode 495. Teemo Attacking (提莫攻击)
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- xml文件的方式实现动态代理基于SpringAOP
1.配置spring容器 导入jar包 com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.j ...
- Python学习之二:Python 与 C 区别
引自http://www.lxway.com/181844.htm 从开始看Python到现在也有半个多月了,前后看了Python核心编程和Dive into Python两本书.话说半个月看两本,是 ...
- Shuffle过程的简单介绍
Shuffle是连接Map和Reduce的桥梁 Shuffle分为Map端的Shuffle和Reduce端的Shuffle Map端的shuffle 1输入数据和执行任务: 分片后分配Map任务,每个 ...