TreeMap和TreeSet的异同:

相同点:

  1. TreeMap和TreeSet都是有序的集合,也就是说他们存储的值都是拍好序的。
  • TreeMap和TreeSet都是非同步集合,因此他们不能在多线程之间共享,不过可以使用方法Collections.synchroinzedMap()来实现同步
  • 运行速度都要比Hash集合慢,他们内部对元素的操作时间复杂度为O(logN),而HashMap/HashSet则为O(1)。

不同点:

  1. 最主要的区别就是TreeSet和TreeMap非别实现Set和Map接口
  • TreeSet只存储一个对象,而TreeMap存储两个对象Key和Value(仅仅key对象有序)
  • TreeSet中不能有重复对象,而TreeMap中可以存在

TreeSet的是NavigableSet的实现类,NavigableSet继承了SortedSet接口,SortedSet是Set的子接口;

 1 public class TreeSet<E> extends AbstractSet<E>
2 implements NavigableSet<E>, Cloneable, java.io.Serializable
3 {
4   /**
5 * The backing map.
6 */
7   private transient NavigableMap<E,Object> m;
8
9 // Dummy value to associate with an Object in the backing Map
10 private static final Object PRESENT = new Object();
11
12   /**
13 * Constructs a set backed by the specified navigable map.
14 */
15 TreeSet(NavigableMap<E,Object> m) {
16 this.m = m;
17 }
18
19 /**
20 * Constructs a new, empty tree set, sorted according to the
21 * natural ordering of its elements. All elements inserted into
22 * the set must implement the {@link Comparable} interface.
23 * Furthermore, all such elements must be <i>mutually
24 * comparable</i>: {@code e1.compareTo(e2)} must not throw a
25 * {@code ClassCastException} for any elements {@code e1} and
26 * {@code e2} in the set. If the user attempts to add an element
27 * to the set that violates this constraint (for example, the user
28 * attempts to add a string element to a set whose elements are
29 * integers), the {@code add} call will throw a
30 * {@code ClassCastException}.
31 */
32 public TreeSet() {
33 this(new TreeMap<E,Object>());
34 }
35   .......
36 }

由上面的TreeSet的源码可以看出,TreeSet的底层实现是通过TreeMap实现的,而TreeMap的底层又是如何实现的呢?

 1   public TreeMap() {
2 comparator = null;
3 }
4
5 public TreeMap(Comparator<? super K> comparator) {
6 this.comparator = comparator;
7 }
8 .....(其他构造方法不一一列举)
9 //这里列举put方法详细讲解
10 public V put(K key, V value) {
11 Entry<K,V> t = root;
12 if (t == null) {
13 compare(key, key); // type (and possibly null) check
14
15 root = new Entry<>(key, value, null);
16 size = 1;
17 modCount++;
18 return null;
19 }
20 int cmp;
21 Entry<K,V> parent;
22 // split comparator and comparable paths
23 Comparator<? super K> cpr = comparator;
24 if (cpr != null) {
25 do {
26 parent = t;
27 cmp = cpr.compare(key, t.key);
28 if (cmp < 0)
29 t = t.left;
30 else if (cmp > 0)
31 t = t.right;
32 else
33 return t.setValue(value);
34 } while (t != null);
35 }
36 else {
37 if (key == null)
38 throw new NullPointerException();
39 Comparable<? super K> k = (Comparable<? super K>) key;
40 do {
41 parent = t;
42 cmp = k.compareTo(t.key);
43 if (cmp < 0)
44 t = t.left;
45 else if (cmp > 0)
46 t = t.right;
47 else
48 return t.setValue(value);
49 } while (t != null);
50 }
51 Entry<K,V> e = new Entry<>(key, value, parent);
52 if (cmp < 0)
53 parent.left = e;
54 else
55 parent.right = e;
56 fixAfterInsertion(e);
57 size++;
58 modCount++;
59 return null;
60 }

从上面的TreeMap的两个构造方法和插入方法可以看出当第一次插入时,返回null,插入值不同时返回null;否则返回值不为null;这里需要注意以下几点:

1、创 建TreeSet或者TreeMap时候采用有参构造函数并且参数是Comparator时候,参数必须是Comparator的实现子类;而利用无参构 造函数时,向TreeSet或者TreeMap添加元素是需要特别注意所添加的对象必须是实现了Comparable接口的子类否则会报错(对象类型 cannot be cast to java.lang.Comparable),这也是TreeMap的put方法中实现的原因,这是多态的表现,父类对象指向子类引用;

Comparable<? super K> k = (Comparable<? super K>) key;

2、由于TreeSet和TreeMap的底层都是树形结构,而且每一个节点的对象是Entry对象

1         K key;
2 V value;
3 Entry<K,V> left = null;
4 Entry<K,V> right = null;
5 Entry<K,V> parent;

这是Entry的结构,是一个类似链表节点的树形结构;

3、TreeSet和TreeMap的底层都是树形结构是一个二叉查找树,并且是一个红黑平衡树,实现方法:

 fixAfterInsertion(e);

Java 集合类 TreeSet、TreeMap的更多相关文章

  1. java集合类之TreeMap

    转自:http://blog.csdn.net/chenssy/article/details/26668941 TreeMap的实现是红黑树算法的实现,所以要了解TreeMap就必须对红黑树有一定的 ...

  2. java集合类TreeMap和TreeSet

    看这篇博客前,可以先看下下列这几篇博客 Red-Black Trees(红黑树)                                         (TreeMap底层的实现就是用的红黑 ...

  3. 关于java集合类TreeMap的理解(转)

    概要 这一章,我们对TreeMap进行学习. 转载请注明出处:http://www.cnblogs.com/skywang12345/admin/EditPosts.aspx?postid=33109 ...

  4. Java集合类--温习笔记

    最近面试发现自己的知识框架有好多问题.明明脑子里知道这个知识点,流程原理也都明白,可就是说不好,不知道是自己表达技能没点,还是确实是自己基础有问题.不管了,再巩固下基础知识总是没错的,反正最近空闲时间 ...

  5. 做JavaWeb开发不知Java集合类不如归家种地

    Java作为面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储.但是使用数组存储对象方面具有一些弊端,而Java 集合就像一种容器,可以动态地把多个对象的引用放入容 ...

  6. 【转载】Java集合类Array、List、Map区别和联系

    Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...

  7. 摘抄转载前辈们的Java集合类总结

    本文摘自 Blue Sky:http://www.cnblogs.com/hubcarl JAVA 集合类介绍和使用 类关系示意图Iterable(接口) │ └--Collection (接口) ├ ...

  8. Java集合类: Set、List、Map、Queue使用场景梳理

    本文主要关注Java编程中涉及到的各种集合类,以及它们的使用场景 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E ...

  9. Java 集合类详解(含类图)

    0.参考文献 此图中蓝色为抽象类.深红色表示接口(Arrays除外).绿色表示具体容器类 1.java集合类图 1.1 1.2 上述类图中,实线边框的是实现类,比如ArrayList,LinkedLi ...

随机推荐

  1. 这些年学过的FPGA

    这些年学过的FPGA 最近看了老罗的鄙视链是怎样炼成的,联想到FPGA.从2011年底开始接触FPGA到现在已经快接近4个年头了,这四年见证了Altera-FPGA的发展,使用的cyclone系列的芯 ...

  2. setTimeout,setInterval原理

    function a() { setTimeout(function(){alert(1)},0); alert(2); } a(); 和其他的编程语言一样,Javascript中的函数调用也是通过堆 ...

  3. 利用ucenter整合discuz数据

    由于项目需要,需要用到discuz论坛用户的数据,所以想到利用ucenter获取数据.以下为整合ucenter的过程. 1.首先你需要下载官方demo:http://faq.comsenz.com/l ...

  4. 使用css3 filter 实现移入背景变色效果

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  5. Bzoj1208 [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7457  Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...

  6. [C#] 使用NPOI将Datatable保存到Excel

    using (table) { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); IRow ...

  7. 利用chorme调试手机网页

    太方便了,很实用的技巧(特意记录一下) 1.pc端安装最新的chrome 2.手机端安装最新的chrome ( Android机 ) 3.USB连接线 4.打开电脑的chrome 在地址栏输入 chr ...

  8. poj1006Biorhythms(同余定理)

    转自:http://blog.csdn.net/dongfengkuayue/article/details/6461298 本文转自head for better博客,版权归其所有,代码系本人自己编 ...

  9. StringBuffer类的功能

    StringBuffer类 1.添加功能 public StringBuffer append(String str):可以把任意类型数据添加到缓冲区,并返回缓冲区域 public StringBuf ...

  10. ypzl药品质量不合格数据库-excel自动排版

    原创: qq:231469242 import xlrdimport pandas,numpyimport matplotlib.pyplot as pltimport pandas as pd #参 ...