Red Black Tree java.util.TreeSet
https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html
- public interface SortedMap<K,V>
- extends Map<K,V>
Map
that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator
typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet
, keySet
and values
methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of SortedSet
.)
All keys inserted into a sorted map must implement the Comparable
interface (or be accepted by the specified comparator). Furthermore, all such keys must be mutually comparable: k1.compareTo(k2)
(or comparator.compare(k1, k2)
) must not throw a ClassCastException
for any keys k1
and k2
in the sorted map. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException
.
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if the sorted map is to correctly implement the Map
interface. (See the Comparable
interface or Comparator
interface for a precise definition of consistent with equals.) This is so because the Map
interface is defined in terms of the equals
operation, but a sorted map performs all key comparisons using its compareTo
(or compare
) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a tree map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map
interface.
All general-purpose sorted map implementation classes should provide four "standard" constructors. It is not possible to enforce this recommendation though as required constructors cannot be specified by interfaces. The expected "standard" constructors for all sorted map implementations are:
- A void (no arguments) constructor, which creates an empty sorted map sorted according to the natural ordering of its keys.
- A constructor with a single argument of type
Comparator
, which creates an empty sorted map sorted according to the specified comparator. - A constructor with a single argument of type
Map
, which creates a new map with the same key-value mappings as its argument, sorted according to the keys' natural ordering. - A constructor with a single argument of type
SortedMap
, which creates a new sorted map with the same key-value mappings and the same ordering as the input sorted map.
Note: several methods return submaps with restricted key ranges. Such ranges are half-open, that is, they include their low endpoint but not their high endpoint (where applicable). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor of a given key, merely request the subrange from lowEndpoint
to successor(highEndpoint)
. For example, suppose that m
is a map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m
whose keys are between low
and high
, inclusive:
- SortedMap<String, V> sub = m.subMap(low, high+"\0");
A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in m
whose keys are between low
and high
, exclusive:
- SortedMap<String, V> sub = m.subMap(low+"\0", high);
This interface is a member of the Java Collections Framework.
- Since:
- 1.2
https://docs.oracle.com/javase/9/docs/api/java/util/NavigableMap.html
- public interface NavigableMap<K,V>
- extends SortedMap<K,V>
SortedMap
extended with navigation methods returning the closest matches for given search targets. Methods lowerEntry(K)
, floorEntry(K)
, ceilingEntry(K)
, and higherEntry(K)
return Map.Entry
objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning null
if there is no such key. Similarly, methods lowerKey(K)
, floorKey(K)
, ceilingKey(K)
, and higherKey(K)
return only the associated keys. All of these methods are designed for locating, not traversing entries.https://docs.oracle.com/javase/9/docs/api/java/util/TreeMap.html
- public class TreeMap<K,V>
- extends AbstractMap<K,V>
- implements NavigableMap<K,V>, Cloneable, Serializable
NavigableMap
implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator
provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey
, get
, put
and remove
operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals
if this sorted map is to correctly implement the Map
interface. (See Comparable
or Comparator
for a precise definition of consistent with equals.) This is so because the Map
interface is defined in terms of the equals
operation, but a sorted map performs all key comparisons using its compareTo
(or compare
) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals
; it just fails to obey the general contract of the Map
interface.
Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedSortedMap
method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
- SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
The iterators returned by the iterator
method of the collections returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove
method, the iterator will throw a ConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException
on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
All Map.Entry
pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue
method. (Note however that it is possible to change mappings in the associated map using put
.)
This class is a member of the Java Collections Framework.
- Since:
- 1.2
- https://docs.oracle.com/javase/9/docs/api/java/util/TreeSet.html
-
- public class TreeSet<E>
- extends AbstractSet<E>
- implements NavigableSet<E>, Cloneable, Serializable
ANavigableSet
implementation based on aTreeMap
. The elements are ordered using their natural ordering, or by aComparator
provided at set creation time, depending on which constructor is used.This implementation provides guaranteed log(n) time cost for the basic operations (
add
,remove
andcontains
).Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the
Set
interface. (SeeComparable
orComparator
for a precise definition of consistent with equals.) This is so because theSet
interface is defined in terms of theequals
operation, but aTreeSet
instance performs all element comparisons using itscompareTo
(orcompare
) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of theSet
interface.Note that this implementation is not synchronized. If multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the
Collections.synchronizedSortedSet
method. This is best done at creation time, to prevent accidental unsynchronized access to the set:- SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
The iterators returned by this class's
iterator
method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's ownremove
method, the iterator will throw aConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw
ConcurrentModificationException
on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.This class is a member of the Java Collections Framework.
- Since:
- 1.2
- See Also:
Collection
,Set
,HashSet
,Comparable
,Comparator
,TreeMap
, Serialized Form
- public class TreeSet<E>
- public class TreeMap<K,V>
- extends AbstractMap<K,V>
- implements NavigableMap<K,V>, Cloneable, Serializable
NavigableMap
implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator
provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey
, get
, put
and remove
operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals
if this sorted map is to correctly implement the Map
interface. (See Comparable
or Comparator
for a precise definition of consistent with equals.) This is so because the Map
interface is defined in terms of the equals
operation, but a sorted map performs all key comparisons using its compareTo
(or compare
) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals
; it just fails to obey the general contract of the Map
interface.
Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedSortedMap
method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
- SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
The iterators returned by the iterator
method of the collections returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove
method, the iterator will throw a ConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException
on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
All Map.Entry
pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue
method. (Note however that it is possible to change mappings in the associated map using put
.)
This class is a member of the Java Collections Framework.
- Since:
- 1.2
Red Black Tree java.util.TreeSet的更多相关文章
- java.util.TreeSet源码分析
TreeSet是基于TreeMap实现的,元素的顺序取决于元素自身的自然顺序或者在构造时提供的比较器. 对于add,remove,contains操作,保证log(n)的时间复杂度. 因为Set接口的 ...
- java.util 类 TreeSet<E>
java.lang.Object≥ java.util.AbstractCollection<E> ≥ java.util.AbstractSet<E> ≥ java.util ...
- Java API —— TreeSet类
1.TreeSet类 1)TreeSet类概述 使用元素的自然顺序对元素进行排序 或者根据创建 set 时提供的 Comparator 进行排序 ...
- 【Java】Treeset实现自定义排序
两个类,一个学生类,含姓名和出生日期两个属性:还有一个学生排序类,重写compare函数,自定义排序规则是先比较出生日期,如果相同再比较姓名字母 package birthday; import ja ...
- 软件包 java.util 的分层结构
概述 软件包 类 使用 树 已过时 索引 帮助 JavaTM Platform Standard Ed. 6 上一个 下一个 框架 无框架 所有类 ...
- JDK的帧--java.util包装工具库
题词 JDK,Java Development Kit. 首先,我们必须认识到,,JDK但,但设置Java只有基础类库.它是Sun通过基础类库开发,这是唯一的.JDK书写总结的类库.从技术含量来说,还 ...
- 【java】TreeSet、Comparable、Comparator、内部类、匿名类
package com.tn.treeSet; public class Student { private String name; private int age; public Student( ...
- Java基础——TreeSet
TreeSet是SortedSet接口的实现类,Sorted的英文意思是分类的:选择的. TreeSet可以确保集合元素处于排序状态.与HashSet集合相比,TreeSet还提供了如下几个额外方法: ...
- java.util.Collection List与其子类 Set与其子类
package com.Collection; import java.util.ArrayList; import java.util.Collection; import java.util.It ...
随机推荐
- Yii查看(输出)当前页面执行的sql语句
在Yii框架下查看当前页面执行的所有sql语句的方法,主要是通过配置相关文件来达到调试sql的目的,具体方法如下: (1)修改 index.php 开启调试模式 在 index.php 文件内增加如下 ...
- codeforces gym 100825 D Rings
这题果然就是个暴力题.... 看每个T的四个方向,有'.',或者在边界上就填1 不然就填四个方向上最小的那个数再加1 然而写wa了几发,有点蠢... #include <bits/stdc++. ...
- Hibernate游记——装备篇《二》(基础配置示例)
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- Codeforces 932 B.Recursive Queries-前缀和 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))
B. Recursive Queries time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 洛谷——P1331 海战
P1331 海战 题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防 ...
- SVG动画基础篇
参考资料: http://www.w3school.com.cn/svg/index.asp https://msdn.microsoft.com/zh-cn/library/gg193979 gi ...
- HTML5 Canvas实现360度全景图
原文:http://blog.csdn.net/jia20003/article/details/17172571 很多购物网站现在都支持360实物全景图像,可以360度任意选择查看样品,这样 对购买 ...
- uibutton 使用settitle后如何修改其中文字对齐方式
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(5, s ...
- 为了安全,linux下如何使用某个用户启动某个进程?
安全里有个原则,叫最小权限原则 根据这个原则,对于启动某个应用或者进程,应该赋予其最小权限,根据应用权限要求,创建一个相应权限的用户,赋予其应用相应的权限,然后使用这个用户启用这个应用 如何使用某个用 ...
- 【windows socket+UDPserverclient】
Windows Socket+UDPserverclient Winsock是 Windows下套接字标准. 1.UDP socket编程: ...