public class TreeSet<E> {
/**
* 用于对该集合中的元素进行排序的比较器,如果该集合使用其元素的自然顺序,则为null
* @return
* 返回用于对该集合中的元素进行排序的比较器如果该集合使用其元素的自然排序,则返回null。
*/
public Comparator<? super E> comparator() {
return tree.comparator() ;
}
/**
* 一个数组,其运行时组件类型为Object,包含此集合中的所有元素
* @return
* 返回包含此集合中所有元素的数组。
*/
public Object[] toArray(){
return tree.toArray() ;
}
/**
* 此集合中元素大于或等于fromElement的部分的视图
* @param fromElement
* 返回集的低端
* @param inclusive
* 如果要在返回的视图中包括低端端点,则为true
* @return
* 返回此集合中元素大于(或等于,如果inclusive为true)fromElement的部分的视图。
*/
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return tree.tailSet(fromElement, inclusive);
}
/**
* 此集合中元素大于或等于fromElement的部分的视图
* @param fromElement
* 返回集的低端(含低端)
* @return
* 返回此集合中元素大于或等于fromElement的部分的视图。
*/
public SortedSet<E> tailSet(E fromElement) {
return tree.tailSet(fromElement) ;
}
public SortedSet<E> subSet(E e1 , E e2) {
return tree.subSet(e1, e2) ;
}
/**
* @return
* 返回此集合中的元素数(其基数)。
*/
public int size() {
return tree.size() ;
}
/**
* 从该映射中删除密钥的映射(可选操作)。
* @param e
* 要从映射中删除其映射的键
* @return
* 与key关联的上一个值,如果key没有映射,则为null。
*/
public boolean remove(E e) {
return tree.remove(e) ;
}
/**
* 检索并删除最后一个(最高)元素,如果此集合为空,则返回null。
* @return
* 最后一个元素,如果此集合为空,则为null
*/
public E pollLast() {
return tree.pollLast() ;
}
/**
* 检索并删除第一个(最低)元素,如果此集合为空,则返回null。
* @return
* 第一个元素,如果此集合为空,则为null
*/
public E pollFirst() {
return tree.pollFirst() ;
}
/**
* 返回该集合中最大的元素,该元素严格小于给定的元素,如果没有这样的元素,则返回null。
* @param e
* 要匹配的值
* @return
* 小于e的最大元素,如果没有这样的元素,则为null
*/
public E lower(E e) {
return tree.lower(e) ;
}
/**
* 返回此映射中当前的最后一个(最高)键
* @return
* 此地图中当前的最后一个(最高)键
*/
public E last() {
return tree.lastKey() ;
}
/**
* 如果此映射不包含键值映射,则返回{@code true}。
* @return
* {@code true}如果此映射不包含键值映射
*/
public boolean isEmpty() {
return tree.isEmpty() == true ;
}
/**
* 大于e的最小元素,如果没有这样的元素,则为null
*/
public E higher(E e) {
return tree.higher(e) ;
}
/**
* @return
* 小于或等于e的最大元素,如果没有这样的元素,则为null
*/
public E floor(E e) {
return tree.floor(e) ;
}
/**
* @return
* 此集合中当前的第一个(最低)元素
*/
public E first() {
return tree.first() ;
}
/**
* @return
* 如果此集合包含指定的元素,则为true
*/
public boolean contains(Object o) {
return tree.contains(o) ;
}
/**
* 回此集合中大于或等于给定元素的最小元素,如果没有此类元素,则返回null。
* @param e
* 要匹配的值
* @return
* 大于或等于e的最小元素,如果没有这样的元素,则为null
*/
public E ceiling(E e) {
return tree.ceiling(e) ;
}
/**
* 返回此{@code TreeSet1}实例的浅层副本。
* @return
* 复制品
*/
public Object clone() {
return tree.clone() ;
}
/**
* 如果指定的元素尚未存在,则将其添加到此集合。
* @param e
* 要检查此集合中是否包含的对象
* @return
* 如果此集合包含指定的元素,就返回true
*/
public boolean add(E e) {
return tree.add(e) ;
}
private transient TreeSet1<E> tree ;
public TreeSet() {
this.tree = new TreeSet1<>() ;
}
public synchronized String toString() {
return tree.toString() ;
}
public Iterator<E> integer() {
return tree.iterator() ;
}
/**
* 集合器
* @author Administrator
*
* @param <E>
*/
public static class TreeSet1<E> extends AbstractSet<E> implements NavigableSet<E> ,Cloneable{
/**
* 用于对该集合中的元素进行排序的比较器,如果该集合使用其元素的自然顺序,则为null
* @return
* 返回用于对该集合中的元素进行排序的比较器
* 如果该集合使用其元素的自然排序,则返回null。
*/
public Comparator<? super E> comparator() {
return m.comparator() ;
}
/**
* 此集合中元素大于或等于fromElement的部分的视图
* @param fromElement
* 返回集的低端
* @param inclusive
* 如果要在返回的视图中包括低端端点,则为true
* @return
* 返回此集合中元素大于(或等于,如果inclusive为true)fromElement的部分的视图。
*/
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new TreeSet1<>(m.tailMap(fromElement, inclusive));
}
/**
* 此集合中元素大于或等于fromElement的部分的视图
* @param fromElement
* 返回集的低端(含低端)
* @return
* 返回此集合中元素大于或等于fromElement的部分的视图。
*/
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
/**
* 此集合中元素范围从包含的元素到排除的元素部分的视图
* @param fromElement
* 返回集的低端
* @param fromInclusive
* 如果要在返回的视图中包括低端端点,则为true
* @param toElement
* 返回集的高端
* @param toInclusive
* 如果要在返回的视图中包括高端端点,则为true
* @return
* 返回此集合中元素范围从Element到toElement的部分的视图。
* 如果fromElement和toElement相等,则返回的集为空
*/
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,E toElement,boolean toInclusive) {
return new TreeSet1<>(m.subMap(fromElement, fromInclusive,toElement,toInclusive));
}
/**
* 此集合中的元素范围从包含的元素到排除的元素部分的视图
* @param fromElement
* 返回集的低端(含低端)
* @param toElement
* 返回集的高端(独占)
* @return
* 返回此集合中元素范围从fromElement,inclusive到toElement,
* exclusive的部分的视图。(如果fromElement和toElement相等,则返回的集为空。)
*/
public SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
/**
* 从该映射中删除密钥的映射(可选操作)。
* @param o
* 要从映射中删除其映射的键
* @return
* 与key关联的上一个值,如果key没有映射,则为null。
*/
public boolean remove(Object o) {
return m.remove(o)==PRESENT;
}
/**
* 检索并删除最后一个(最高)元素,如果此集合为空,则返回null。
* @return
* 最后一个元素,如果此集合为空,则为null
*/
public E pollLast() {
Map.Entry<E,?> e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
/**
* 检索并删除第一个(最低)元素,如果此集合为空,则返回null。
* @return
* 第一个元素,如果此集合为空,则为null
*/
public E pollFirst() {
Map.Entry<E,?> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
/**
* 返回该集合中最大的元素,该元素严格小于给定的元素,如果没有这样的元素,则返回null。
* @param e
* 要匹配的值
* @return
* 小于e的最大元素,如果没有这样的元素,则为null
*/
public E lower(E e) {
return m.lowerKey(e) ;
}
/**
* 返回此映射中当前的最后一个(最高)键。
* @return
* 此地图中当前的最后一个(最高)键
*/
public E lastKey() {
return m.lastKey() ;
}
/**
* 如果此集合不包含任何元素,则返回true。
* @return
* 如果此集合不包含元素,则为true
*/
public boolean isEmpty() {
return m.isEmpty() ;
}
/**
* 返回此集合中的元素数(其基数)。
*/
public int size() {
return m.size();
}
TreeMap1<E,Object> map ;
/**
* @return
* 大于e的最小元素,如果没有这样的元素,则为null
*/
public E higher(E e) {
return m.higherKey(e) ;
}
/**
* @return
* 小于或等于e的最大元素,如果没有这样的元素,则为null
*/
public E floor(E e) {
return m.floorKey(e) ;
}
/**
* @return
* 此集合中当前的第一个(最低)元素
*/
public E first() {
return m.firstKey() ;
}
/**
* 如果此集合包含指定的元素,则返回{@code true}。
* @param o
* @return
*/
public boolean contains(Object o) {
return m.containsKey(o);
}
/**
* 回此集合中大于或等于给定元素的最小元素,如果没有此类元素,则返回null。
* @param e
* 要匹配的值
* @return
* 大于或等于e的最小元素,如果没有这样的元素,则为null
*/
public E ceiling(E e) {
return m.ceilingKey(e) ;
}
@SuppressWarnings("unchecked")
public Object clone() {
TreeSet1<E> clone;
try {
clone = (TreeSet1<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
/*构造一个新的树映射,其中包含与指定的排序映射相同的映射,
* 并使用相同的顺序,可以不要这个*/
clone.m = new TreeMap1<>(m);
return clone;
}
private static final Object PRESENT = new Object() ;
/**
* 背景地图。
*/
private transient NavigableMap<E, Object> m ;
public TreeSet1(NavigableMap<E,Object> m) {
this.m = m ;
}
public TreeSet1() {
this(new TreeMap1<>()) ;
}
public boolean add( E e ) {
return m.put(e , PRESENT) == null ;
}
public Iterator<E> iterator() {
return m.navigableKeySet().iterator() ;
}
}
public interface SortedMap<K,V> extends Map<K,V>{
/**
* 用于对该映射中的键进行排序的比较器,如果该映射使用其键的自然顺序,则为null
* @return
*/
Comparator<? super K> comparator();
/**
* 返回此映射中当前的第一个(最低)键。
* @return
* 此地图中当前的第一个(最低)键
*/
K firstKey();
/**
* 返回此映射中当前的最后一个(最高)键
* @return
* 此地图中当前的最后一个(最高)键
*/
K lastKey();
}
public interface NavigableMap<K,V> extends SortedMap<K,V>{
/**
* 此地图中关键帧大于(或等于,如果包含为真)fromKey部分的视图
* @param fromkey
* 返回的映射中键的低端
* @param inclusive
* 如果要在返回的视图中包括低端端点,则为true
* @return
* 返回此映射中键大于(或等于,如果inclusive为true)fromKey的部分的视图。
*/
NavigableMap<K,V> tailMap(K fromkey , boolean inclusive) ;
/**
* 返回此地图中关键帧范围从fromKey到toKey的部分的视图。
* 如果fromKey和toKey相等,则返回的映射为空
* @param fromKey
* 返回的映射中键的低端
* @param fromInclusive
* 如果要在返回的视图中包括低端端点,则为true
* @param toKey
* 返回的映射中键的高端
* @param toInclusive
* 如果要在返回的视图中包括高端端点,则为true
* @return
* 此地图的关键帧范围从from Key到toKey的部分视图
*/
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,K toKey, boolean toInclusive);
/**
* 移除并返回与此映射中最大键关联的键值映射,如果映射为空,则返回null。
* @return
* 已删除此映射的最后一个条目,如果此映射为空,则为null
*/
Map.Entry<K,V> pollLastEntry();
/**
* 移除并返回与此映射中最小键关联的键值映射,如果映射为空,则返回{@code null}。
* @return
* 已删除此映射的第一个条目,如果此映射为空,则为null
*/
Map.Entry<K,V> pollFirstEntry();
/**
* 返回严格小于给定密钥的最大密钥,如果没有此类密钥,则返回null。
* @param key
* 钥匙
* @return
* 最大密钥小于key,如果没有这样的密钥,则为null
*/
K lowerKey(K key);
/**
* 返回此映射中包含的键的{@link NavigableSet}视图。集合的迭代器按升序返回键
* @return
*/
NavigableSet<K> navigableKeySet() ;
K ceilingKey(K key);
/**
* @param key
* 钥匙
* @return
* 最大密钥小于或等于{@code key},或{@code null},如果没有这样的密钥
*/
K floorKey(K key);
/**
* @return
* 最小密钥大于密钥,如果没有这样的密钥,则为null
*/
K higherKey(K key);
}
public interface SortedSet<E> extends Set<E>{}
public interface NavigableSet<E> extends SortedSet<E>{
E ceiling(E e);
}
public static final class Integer {
public static final int MAX_VALUE = 0x7fffffff;
/**
* 指定int值的两位补码二进制表示中最高阶(“最左边”)前一位的零位数,如果值等于零,则为32位
* 如果指定的值在其2的补码表示中没有一位,即等于零,则返回32
* <p>
* 请注意,此方法与以2为底的对数密切相关。对于所有正整数x值:
* <ul>
* <li>floor(log2(x)) = 31 - numberOfLeadingZeros(x)
* <li>ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1)
* @param i
* 要计算其前导零数的值
* @return
* 返回指定int值的二元补码,二进制表示中最高阶(“最左边”)一位之前的零位数
*
*/
public static int numberOfLeadingZeros(int i) {
if (i <= 0)
return i == 0 ? 32 : 0;
int n = 31;
if (i >= 1 << 16) { n -= 16; i >>>= 16; }
if (i >= 1 << 8) { n -= 8; i >>>= 8; }
if (i >= 1 << 4) { n -= 4; i >>>= 4; }
if (i >= 1 << 2) { n -= 2; i >>>= 2; }
return n - (i >>> 1);
}
}
public static class ArraysSupport{
/**
*
* @param oldLength
* @param minGrowth
* @return
*/
private static int hugeLength(int oldLength, int minGrowth) {
int minLength = oldLength + minGrowth;
if (minLength < 0) { // overflow
throw new OutOfMemoryError("Required array length too large");
}
if (minLength <= MAX_ARRAY_LENGTH) {
return MAX_ARRAY_LENGTH;
}
return Integer.MAX_VALUE;
}
public static final int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8;
/**
* 根据数组的当前长度、preferredgrowth值和最小增长值计算新数组长度。
* @param oldLength
* 数组的当前长度(必须为非负)
* @param minGrowth
* 阵列长度所需的最小增长(必须为正)
* @param prefGrowth
* 数组长度的首选增长(忽略,如果小于minGrowth)
* @return
* 数组的新长度
*/
public static int newLength(int oldLength, int minGrowth, int prefGrowth) {
int newLength = Math1.max(minGrowth, prefGrowth) + oldLength;
if (newLength - MAX_ARRAY_LENGTH <= 0) {
return newLength;
}
return hugeLength(oldLength, minGrowth);
}
}
public static final class Array{
private static native Object newArray(Class<?> componentType, int length) throws NegativeArraySizeException;
/**
* 创建具有指定组件类型和长度的新数组。调用此方法相当于创建arrayas
* <pre>
* int[] x = {length};
* Array.newInstance(componentType, x);
* </pre>
* 新数组的维数不能超过255。
* @param componentType
* @param length
* @return
* @throws NegativeArraySizeException
*/
public static Object newInstance(Class<?> componentType, int length) throws NegativeArraySizeException {
return newArray(componentType, length);
}
}
public static final class Math1{
/**
* a和b中较大的一个。
* @param a
* 第一个论点
* @param b
* 另一个论点
* @return
* 返回两个int值中的较大值。
*/
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
/**
* a和b中较小的一个。
* @param a
* 第一个论点
* @param b
* 另一个论点。
* @return
* 返回a和b中较小的一个。
*/
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
}
public static class Arrays{
/**
* 复制指定的数组,截断或填充空值(如有必要),使副本具有指定的长度。
* @param <T>
* 返回数组中对象的类
* @param <U>
* 原始数组中对象的类
* @param original
* 要复制的数组
* @param newLength
* 要返回的副本的长度
* @param newType
* 要返回的副本的类
* @return
* 返回原始数组的副本,截断或填充为空以获得指定的长度
*/
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,Math1.min(original.length, newLength));
return copy;
}
/**
* 复制指定的数组,截断或填充空值(如有必要),使副本具有指定的长度。
* @param <T>
* 数组中对象的类
* @param original
* 要复制的数组
* @param newLength
* 要返回的副本的长度
* @return
* 原始数组的副本,截断或填充为空以获得指定的长度
*/
@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
}
public static abstract class AbstractCollection<E> implements Set<E>{
/**
* 当迭代器返回的元素超过预期时,重新分配toArray中使用的数组,并从迭代器中完成填充。
* @param <T>
* @param r
* 数组中充满了以前存储的元素
* @param it
* 此集合上正在进行的迭代器
* @return
* 返回数组,其中包含给定数组中的元素,以及迭代器返回的任何其他元素,这些元素被修剪为大小
*/
/*219*/
@SuppressWarnings("unchecked")
private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
int len = r.length;
int i = len;
while (it.hasNext()) {
if (i == len) {
len = ArraysSupport.newLength(len, 1, /* minimum growth */ (len >> 1) + 1 /* preferred growth */);
r = Arrays.copyOf(r, len);
}
r[i++] = (T)it.next();
}
// trim if overallocated
return (i == len) ? r : Arrays.copyOf(r, i);
}
/**
* 一个数组,其运行时组件类型为Object,包含此集合中的所有元素.
* @return
* 返回包含此集合中所有元素的数组。
*/
public Object[] toArray() {
Object[] r = new Object[size()];
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext())
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
/**
* 返回此集合中包含的元素的迭代器。
*/
public abstract Iterator<E> iterator();
public String toString() {
Iterator<E> it = iterator();
if (!it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
}
public static abstract class AbstractSet<E> extends AbstractCollection<E>{}
public interface Iterator<E> {
boolean hasNext() ;
/**
* @return
* 返回迭代中的下一个元素。
*/
E next() ;
}
public interface Set <E>{
Iterator<E> iterator() ;
int size();
}
public interface Map<K,V> {
/**
* 从该映射中删除密钥的映射(可选操作)。
* @param key
* 要从映射中删除其映射的键
* @return
* 与key关联的上一个值,如果key没有映射,则为null。
*/
V remove(Object key);
/**
* 如果此映射不包含键值映射,则返回{@code true}。
* @return
* {@code true}如果此映射不包含键值映射
*/
boolean isEmpty();
/**
* 如果此映射包含指定键的映射,则返回{@code true}。
* @param key
* 要测试其在地图中的位置的密钥
* @return
* {@code true}如果此映射包含指定键的映射
*/
boolean containskey(Object key) ;
/**
* @return
* 此映射中的键值映射数
*/
int size();
/**
* 返回此映射中包含的映射的{@link Set}视图。
* @return
*/
Set<Map.Entry<K, V>> entrySet();
interface Entry<K,V> {
K getKey() ;
V getValue() ;
}
V get (Object key) ;
/**
* 如果此映射包含指定对象的映射,则返回{@code true}
* @param key
* 要测试其在地图中的位置的密钥
* @return
*/
boolean containsKey(Object key) ;
/**
* 返回指定键映射到的值,如果此映射不包含该键的映射,则返回{@code defaultValue}。
* @param key
* 指定的键
* @param defaultValue
* 如果此映射不包含该键的映射时要返回的值
* @return
*/
default V getOrDefault(Object key , V defaultValue) {
V v ;
return (( v = get(key)) != null || containsKey(key)) ? v : defaultValue ;
}
/**
* 将指定值与此映射中的指定键关联(可选操作)。
* 如果映射之前包含键的映射,则旧值将替换为指定值。
* @param key
* @param value
* @return
*/
V put(K key ,V value ) ;
}
public static abstract class AbstractMap<K,V> implements Map<K,V> , Cloneable{
/**
* 返回此映射中的键值映射数。
*/
public int size() {
return entrySet().size();
}
/**
* 如果此映射不包含键值映射,则返回true
*/
public boolean isEmpty() {
return size() == 0;
}
public static class SimpleImmutableEntry<K,V> implements Map.Entry<K, V>{
private final K key ;
private final V value ;
/**
* 创建一个表示与指定项相同映射的项。
* @param entry
*/
public SimpleImmutableEntry (Map.Entry<? extends K, ? extends V> entry) {
this.key = entry.getKey() ;
this.value = entry.getValue() ;
}
public K getKey () {
return key ;
}
public V getValue () {
return value ;
}
public synchronized String toString() {
return key + " " + value ;
}
}
/**
* 此映射中包含的映射的集合视图
*/
public abstract Set<Map.Entry<K,V>> entrySet() ;
public synchronized String toString () {
Iterator<Map.Entry<K, V>> i = entrySet().iterator() ;
System.out.println(i==null);
if (!i.hasNext())
return "{}" ;
StringBuilder s = new StringBuilder() ;
s.append("{") ;
for( ; ; ) {
Map.Entry<K, V> e = i.next() ;
K key = e.getKey() ;
V value = e.getValue() ;
s.append(key == this ? "(this Map)" : key.toString()) ;
s.append("=") ;
s.append(value == this ? "(this Map)" : value.toString()) ;
if (!i.hasNext())
return s.append("}").toString() ;
s.append(',').append(' ') ;
}
}
}
public static final class Objects {
public static <T> T nonNull ( T obj) {
if ( obj == null)
throw new NullPointerException() ;
return obj ;
}
}
public interface Comparator <T> {
int compare ( T o1 , T o2) ;
}
public static class TreeMap1<K,V> extends AbstractMap<K,V> implements NavigableMap<K, V> {
public NavigableMap<K,V> tailMap(K fromKey , boolean inclusive) {
return new AscendingSubMap<>(this,false, fromKey, inclusive,true, null, true);
}
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,K toKey,boolean toInclusive) {
return new AscendingSubMap<>(this,false, fromKey, fromInclusive,false, toKey, toInclusive);
}
public Map.Entry<K,V> pollLastEntry() {
Entry<K,V> p = getLastEntry();
Map.Entry<K,V> result = exportEntry(p);
if (p != null)
deleteEntry(p);
return result;
}
public Map.Entry<K,V> pollFirstEntry() {
Entry<K,V> p = getFirstEntry();
Map.Entry<K,V> result = exportEntry(p);
if (p != null)
deleteEntry(p);
return result;
}
/**
* 返回小于指定密钥的最大密钥的条目;如果不存在这样的条目(即树中最小的键大于指定的键),
* 则返回{@code null}。
* @param key
* @return
*/
final Entry<K,V> getLowerEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp > 0) {
if (p.right != null)
p = p.right;
else
return p;
} else {
if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
}
return parent;
}
}
}
return null;
}
public K lowerKey(K key) {
return keyOrNull(getLowerEntry(key));
}
/**
* 返回TreeMap中的最后一个条目(根据TreeMap的skey-sort函数)。
* 如果树映射为空,则返回null。
*/
final Entry<K,V> getLastEntry() {
Entry<K,V> p = root;
if (p != null)
while (p.right != null)
p = p.right;
return p;
}
public K lastKey() {
return key(getLastEntry());
}
public K higherKey(K key) {
return keyOrNull(getHigherEntry(key));
}
/**
* 获取与指定键对应的项;如果不存在这样的条目,则返回小于指定密钥的最大密钥的条目;
* 如果不存在这样的条目,则返回{@code null}。
* @param key
* 指定的行
*/
final Entry<K,V> getFloorEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp > 0) {
if (p.right != null)
p = p.right;
else
return p;
} else if (cmp < 0) {
if (p.left != null) {
p = p.left;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.left) {
ch = parent;
parent = parent.parent;
}
return parent;
}
} else
return p;
}
return null;
}
/**
* 最大密钥小于或等于密钥,如果没有这样的密钥,则为null
*/
public K floorKey(K key) {
return keyOrNull(getFloorEntry(key));
}
/**
* @return
* 返回与指定项对应的键
*/
static <K> K key(Entry<K,?> e) {
if (e==null)
throw new NoSuchElementException();
return e.key;
}
/**
* 此地图中当前的第一个(最低)键
*/
public K firstKey() {
return key(getFirstEntry());
}
/**
* 如果此映射包含指定键的映射,则返回{@code true}。
*/
public boolean containskey(Object key) {
return getEntry(key) != null;
}
/**
* 返回项的键,如果为null,则返回null
* @param <K>
* @param <V>
* @param e
* @return
*/
static <K,V> K keyOrNull(TreeMap1.Entry<K,V> e) {
return (e == null) ? null : e.key;
}
/**
* 大于或等于key的最小key,如果没有这样的key,则为null
* @param key
* 钥匙
*/
public K ceilingKey(K key) {
return keyOrNull(getCeilingEntry(key));
}
/**
* 递归的“助手方法”,完成前一种方法的实际工作。同名参数有相同的定义。
* 其他参数记录如下。假设在调用此方法之前,树映射区域的比较器和大小字段已经设置好。
* (它会忽略这两个字段。)
* @param level
* 树的当前级别。初始呼叫应为0。
* @param lo
* 此子树的第一个元素索引。初始值应为0。
* @param hi
* 此子树的最后一个元素索引。首字母大小应为-1。
* @param redLevel
* 节点应为红色的级别。对于这种大小的树,必须等于computeRedLevel。
* @param it
* @param str
* @param defaultVal
* @return
* @throws java.io.IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
private final Entry<K,V> buildFromSorted(
int level,
int lo,
int hi,
int redLevel,
Iterator<?> it,
java.io.ObjectInputStream str,
V defaultVal)
throws java.io.IOException, ClassNotFoundException {
if (hi < lo) {//询问一下第一个数是否大于最后一个数,映射的总数量
return null;
}
int mid = (lo + hi) >>> 1;
Entry<K,V> left = null;
if (lo < mid) {//
left = buildFromSorted( level+1, lo, mid - 1, redLevel,it, str, defaultVal);
}
// /*从迭代器或流中提取键和/或值*/
K key ; V value ;
if (it != null) {
if (defaultVal==null) {
Map.Entry<?,?> entry = (Map.Entry<?,?>)it.next();
key = (K)entry.getKey();
value = (V)entry.getValue();
} else {
key = (K)it.next();
value = defaultVal;
}
} else {
key = (K) str.readObject();
value = (defaultVal != null ? defaultVal : (V) str.readObject());
}
Entry<K,V> middle = new Entry<>(key, value, null);
if (level == redLevel) {
middle.color = RED;
}
if (left != null) {
middle.left = left;
left.parent = middle;
}
if (mid < hi) {
Entry<K,V> right = buildFromSorted( level+1, mid+1, hi, redLevel,it, str, defaultVal);
middle.right = right;
right.parent = middle;
}
return middle ;
}
private static int computeRedLevel(int size) {
return 31 - Integer.numberOfLeadingZeros(size + 1);
}
/**
* 基于排序数据的线性时间树生成算法。可以接受迭代器或流中的键和/或值。
* size要从迭代器中读取的键(或键值对)的数量,或streamit如果为非空,
* 则从该迭代器中读取的entriesor键创建新条目。str如果不为null,
* 则将从键创建新条目,并可能从该流中以序列化形式读取值。它和str中只有一个应该是非空的。
* defaultVal如果非空,则此默认值用于映射中的每个值。如果为null,
* 则每个值都从迭代器或流中读取
* @param size
* 要从迭代器或流中读取的键(或键值对)的数目
* @param it
* 如果非空,则从该迭代器读取的entriesor键创建新条目。
* @param str
* 如果不为null,则将从键创建新条目,可能还会从该流中以序列化形式读取值。
* 它和str中只有一个应该是非空的。
* @param defaultVal
* 如果非空,则此默认值用于映射中的每个值。如果为null,则每个值都从迭代器或流中读取,如上所述。
* @throws java.io.IOException
* @throws ClassNotFoundException
*/
private void buildFromSorted(int size, Iterator<?> it,java.io.ObjectInputStream str, V defaultVal) throws java.io.IOException, ClassNotFoundException {
this.size = size;
root = buildFromSorted(0, 0, size-1,computeRedLevel(size),it, str, defaultVal);
}
public Comparator<? super K> comparator() {
return comparator;
}
/**
* 构造一个新的树映射,其中包含与指定的排序映射相同的映射,并使用相同的顺序。
* 该方法以线性时间运行。
* @param m
* 将其映射放置在此映射中的已排序映射,以及将使用其比较器对该映射进行排序的已排序映射
*/
public TreeMap1 (SortedMap<K, ? extends V> m) {
comparator = m.comparator();
try {
buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
} catch (java.io.IOException | ClassNotFoundException cannotHappen) {}
}
/**
* 获取大于指定键的最小键的项;如果不存在这样的条目,则返回大于指定密钥的最小密钥的条目;
* 如果不存在这样的条目,则返回{@code null}。
* @param key
* 指定的键
* @return
*/
final Entry<K,V> getHigherEntry(K key) {
Entry<K,V> p = root;
while (p != null) {
int cmp = compare(key, p.key);
if (cmp < 0) {
if (p.left != null )
p = p.left;
else
return p;
} else {
if (p.right != null) {
p = p.right;
} else {
Entry<K,V> parent = p.parent;
Entry<K,V> ch = p;
while (parent != null && ch == parent.right) {
ch = parent;
parent = parent.parent;
}
return parent;
}
}
}
return null;
}
private static final Object UNBOUNDED = new Object();
static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V>{
public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
if (!inRange(fromKey, inclusive))
throw new IllegalArgumentException("fromKey out of range");
return new AscendingSubMap<>(m,false, fromKey, inclusive,toEnd, hi, hiInclusive);
}
AscendingSubMap(TreeMap1<K,V> m, boolean fromStart, K lo, boolean loInclusive,boolean toEnd, K hi, boolean hiInclusive) {
super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
}
Iterator<K> keyIterator() {
return new SubMapKeyIterator(absLowest(),absHighFence());
}
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
if (!inRange(fromKey, fromInclusive))
throw new IllegalArgumentException("fromKey out of range");
if (!inRange(toKey, toInclusive))
throw new IllegalArgumentException("toKey out of range");
return new AscendingSubMap<>(m,false, fromKey, fromInclusive,false, toKey, toInclusive);
}
final class AscendingEntrySetView extends EntrySetView {
public Iterator<Map.Entry<K,V>> iterator() {
return new SubMapEntryIterator(absLowest(), absHighFence());
}
}
public Set<Map.Entry<K,V>> entrySet() {
EntrySetView es = entrySetView;
return (es != null) ? es : (entrySetView = new AscendingEntrySetView());
}
}
abstract static class bleMap<K,V> extends AbstractMap<K, V> implements NavigableMap<K,V>{
public Entry<K, V> pollLastEntry() {
return null;
}
public Entry<K, V> pollFirstEntry() {
return null;
}
public K lowerKey(K key) {
return null;
}
public K ceilingKey(K key) {
return null;
}
public K floorKey(K key) {
return null;
}
public K higherKey(K key) {
return null;
}
public Comparator<? super K> comparator() {
return null;
}
public K firstKey() {
return null;
}
public K lastKey() {
return null;
}
public V remove(Object key) {
return null;
}
public boolean isEmpty() {
return false;
}
public boolean containskey(Object key) {
return false;
}
public int size() {
return 0;
}
public V get(Object key) {
return null;
}
public boolean containsKey(Object key) {
return false;
}
public V put(K key, V value) {
return null;
}
}
abstract static class NavigableSubMap<K,V> extends bleMap<K,V>{
final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K, V>>{
SubMapEntryIterator(TreeMap1.Entry<K,V> first,TreeMap1.Entry<K,V> fence) {
super(first, fence);
}
/**
* 迭代中的下一个元素
* @return
* 返回迭代中的下一个元素
*/
public Map.Entry<K,V> next() {
return nextEntry();
}
}
transient EntrySetView entrySetView;
abstract class EntrySetView extends AbstractSet<Map.Entry<K, V>>{
private transient int size = -1, sizeModCount;
/**
* 返回此集合中的元素数(其基数)。
* @return
* 此集合中的元素数(其基数)
*/
public int size() {
if (fromStart && toEnd)
return m.size();
if (size == -1 || sizeModCount != m.modCount) {
sizeModCount = m.modCount;
size = 0;
Iterator<?> i = iterator();
while (i.hasNext()) {
size++;
i.next();
}
}
return size;
}
}
NavigableSubMap(TreeMap1<K,V> m,boolean fromStart, K lo, boolean loInclusive,boolean toEnd,K hi, boolean hiInclusive) {
if (!fromStart && !toEnd) {
if (m.compare(lo, hi) > 0)
throw new IllegalArgumentException("fromKey > toKey");
} else {
if (!fromStart)
m.compare(lo, lo);
if (!toEnd)
m.compare(hi, hi);
}
this.m = m;
this.fromStart = fromStart;
this.lo = lo;
this.loInclusive = loInclusive;
this.toEnd = toEnd;
this.hi = hi;
this.hiInclusive = hiInclusive;
}
transient KeySet<K> navigableKeySetView;
public final NavigableSet<K> navigableKeySet() {
KeySet<K> nksv = navigableKeySetView;
return (nksv != null) ? nksv : (navigableKeySetView = new KeySet<>(this));
}
final boolean inClosedRange(Object key) {
return (fromStart || m.compare(key, lo) >= 0)
&& (toEnd || m.compare(hi, key) >= 0);
}
final boolean tooLow(Object key) {
if (!fromStart) {
int c = m.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive))
return true;
}
return false;
}
final boolean inRange(Object key) {
return !tooLow(key) && !tooHigh(key);
}
final boolean inRange(Object key, boolean inclusive) {
return inclusive ? inRange(key) : inClosedRange(key);
}
abstract class SubMapIterator<T> implements Iterator<T>{
int expectedModCount;
TreeMap1.Entry<K,V> lastReturned;
TreeMap1.Entry<K,V> next;
final Object fenceKey;
SubMapIterator(TreeMap1.Entry<K,V> first,TreeMap1.Entry<K,V> fence) {
expectedModCount = m.modCount;
lastReturned = null;
next = first;
fenceKey = fence == null ? UNBOUNDED : fence.key;
}
public final boolean hasNext() {
return next != null && next.key != fenceKey;
}
final TreeMap1.Entry<K,V> nextEntry() {
TreeMap1.Entry<K,V> e = next;
if (e == null || e.key == fenceKey)
throw new NoSuchElementException();
if (m.modCount != expectedModCount)
throw new ConcurrentModificationException();
next = successor(e);
lastReturned = e;
return e;
}
}
/*2046*/
final class SubMapKeyIterator extends SubMapIterator<K>{
SubMapKeyIterator(TreeMap1.Entry<K,V> first,TreeMap1.Entry<K,V> fence) {
super(first, fence);
}
public K next() {
return nextEntry().key;
}
}
final boolean tooHigh(Object key) {
if (!toEnd) {
int c = m.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive))
return true;
}
return false;
}
final TreeMap1.Entry<K,V> absLowest() {
TreeMap1.Entry<K,V> e = (fromStart ? m.getFirstEntry() : (loInclusive ? m.getCeilingEntry(lo) : m.getHigherEntry(lo)));
return (e == null /*|| tooHigh(e.key)*/) ? null : e;
}
/**
* @return
* 从这个子映射的角度返回升序迭代器
*/
abstract Iterator<K> keyIterator();
final boolean fromStart, toEnd;
K hi; final boolean loInclusive, hiInclusive;
/**
* 返回升序遍历的绝对高度
* @return
*/
final TreeMap1.Entry<K,V> absHighFence() {
return (toEnd ? null : (!hiInclusive ? m.getHigherEntry(hi) :m.getCeilingEntry(hi)));
}
TreeMap1<K,V> m;
K lo;
}
final class KeyIterator extends PrivateEntry<K>{
KeyIterator(Entry<K,V> first) {
super(first);
}
public K next() {
return nextEntry().key;
}
}
Iterator<K> keyIterator() {
return new KeyIterator(getFirstEntry()) ;
}
static final class KeySet<E> implements NavigableSet<E>{
/**
* 此集合中的元素数(其基数)
*/
public int size() {
return m.size() ;
}
private final NavigableMap<E , ?> m ;
KeySet(NavigableMap<E,?> map){
this.m = map ;
}
/**
* 该集合中元素的迭代器,按升序排列
*/
public Iterator<E> iterator() {
if (m instanceof TreeMap1) {
return ((TreeMap1<E,?>)m).keyIterator() ;
}
else {
return ((TreeMap1.NavigableSubMap<E,? >)m).keyIterator() ;
}
}
public E ceiling(E e) {
return m.ceilingKey(e);
}
}
private transient KeySet<K> navigableKeySet ;
public NavigableSet<K> navigableKeySet() {
KeySet<K> ent = navigableKeySet ;
return ( ent != null) ? ent : (navigableKeySet = new KeySet<>(this)) ;
}
/**
* 将指定值与此映射中的指定键关联(可选操作)。
* 如果映射之前包含键的映射,则旧值将替换为指定值。
*/
public V put(K key , V value) {
return put(key , value , true) ;
}
/**
* 删除此映射中的所有映射。此调用返回后,映射将为空。
*/
public void clear() {
modCount++ ;
size = 0 ;
root = null ;
}
/**
* 返回此映射中的键值映射数。
* @return
*/
public int size() {
return size ;
}
/**
* 删除节点p,然后重新平衡树。
* @param p
* 要删除的节点
*/
private void deleteEntry(Entry<K,V> p) {
modCount++ ;
size-- ;
/*如果是严格内部的,则将继任者的元素复制到p,然后使p指向继任者。*/
if (p.left != null && p.right != null) {
Entry<K,V> s = successor(p) ;
p.key = s.key ;
p.value = s.value ;
p = s ;
}
/*在替换节点(如果存在)启动修复*/
Entry<K,V> replacement = (p.left != null ? p.left : p.right) ;
if ( replacement != null) {
/*将替换链接到父级*/
replacement.parent = p.parent ;
if (p.parent == null )
root = replacement ;
else if (p == p.parent.left)
p.parent.left = replacement ;
else
p.parent.right = replacement ;
/*清空链接,这样它们就可以被FixAfterDelete使用了。*/
p.left = p.right = p.parent = null ;
}else if (p.parent == null )
root = null ;
else {/*没有孩子。将self用作幻影替换并取消链接。*/
if (p.parent != null ) {
if (p == p.parent.left)
p.parent.left = null ;
else if (p == p.parent.left)
p.parent.right = null ;
p.parent = null ;
}
}
}
/**
* 从树映射中删除此键的映射(如果存在)。
* @param key
* 应删除其映射的键
* @return
* 指定的键关联上的一个值
*/
public V remove(Object key) {
Entry<K,V> p = getEntry(key) ;
if ( p == null)
return null ;
V oldValue = p.value ;
deleteEntry(p) ;
return oldValue ;
}
/**
* 与指定键关联的上一个值,如果该键没有映射,则为null
* @param key
* 与指定值关联的键
* @param value
* 要与指定键关联的值
* @return
* 如果指定的键没有被映射 , 就返回 value ,否则返回指定的键关联上的一个值,该方法无法替换原有值
*/
public V putIfAbsent(K key , V value) {
return put(key , value , false) ;
}
/**
* 返回指定键映射到的值,如果此映射不包含该键的映射,则返回{@code null}
*/
public V get(Object key) {
Entry<K,V> p = getEntry(key) ;
return ( p == null ? null : p.value) ;
}
/**
* 测试两个值是否相等。与o1不同。等于(o2),只是因为它能正确地处理{@code null}o1。
* @param o1
* @param o2
* @return
*/
private static final boolean valEquals(Object o1 , Object o2) {
return (o1 == null ? o2 == null : o1.equals(o2)) ;
}
/**
* 返回树映射中的第一个条目(根据树映射的键排序函数)。如果树映射为空,则返回null。
* @return
*/
private final Entry<K,V> getFirstEntry () {
Entry<K,V> p = root ;
if ( p != null )
while ( p.left != null )
p = p.left ;
return p ;
}
/**
* 如果此映射将一个或多个键映射到指定值,则返回{@code true}。
* @param value
* 要测试其在该地图中的存在的值
* @return
*/
public boolean containsValue (Object value) {
for (Entry<K,V> e = getFirstEntry() ; e != null ; e = successor(e) ) {
if (valEquals (value , e.value ))
return true ;
}
return false ;
}
/**
* 使用comparator的getEntry版本。与getEntry分离以获得性能。
* (对于大多数不太依赖比较器性能的方法来说,这是不值得的,但在这里是值得的。)
* @param key
* @return
*/
private final Entry<K,V> getEntryUsingComparator (Object key) {
@SuppressWarnings("unchecked")
K k = (K)key ;
Comparator<? super K> c = comparator ;
if (c != null) {
Entry<K,V> p = root ;
while (p != null ) {
int cmp = c.compare(k,p.key) ;
if (cmp < 0 )
p = p.left ;
else if (cmp > 0 )
p = p.right ;
else
return p ;
}
}
return null ;
}
/**
* 返回给定键的映射项,如果映射不包含该键的项,则返回{@code null}
* @param key
* @return
*/
private final Entry<K,V> getEntry (Object key) {
if ( comparator != null )
return getEntryUsingComparator(key) ;
Objects.nonNull(key) ;
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key ;
Entry<K,V> p = root ;
while(p != null) {
int cmp = k.compareTo(p.key) ;
if ( cmp < 0 )
p = p.left ;
else if ( cmp > 0 )
p = p.right ;
else
return p ;
}
return null ;
}
/**
* 如果此映射包含指定键的映射,则返回{@code true}。
*/
public boolean containsKey ( Object key ) {
return getEntry(key) != null;
}
/**
* 获取与指定键对应的项;如果不存在这样的条目,则返回大于指定密钥的最小密钥的条目;
* 如果不存在这样的条目(即树中最大的键小于指定的键),则返回{@code null}。
* @param key
* @return
*/
final Entry<K,V> getCeilingEntry (K key) {
Entry<K,V> p = root ;
while ( p != null) {
int cmp = compare(key, p.key) ;
if ( cmp < 0 ) {
if ( p.left != null )
p = p.left ;
else
return p ;
}else if ( cmp > 0 ) {
if ( p .right != null) {
p = p.right ;
} else {
Entry<K,V> parent = p.parent ;
Entry<K,V> ch = p ;
while ( parent != null && ch == parent.right) {
ch = parent ;
parent = parent.parent ;
}
return parent ;
}
}else {
return p ;
}
}
return null ;
}
final Entry<K,V> getCei(K key){
Entry<K,V> t = root ;
@SuppressWarnings("unchecked")
Comparable<? super K> b = (Comparable<? super K>)key ;
while(t != null) {
@SuppressWarnings("unchecked")
Comparable<? super K> u = (Comparable<? super K>)t.key ;
if(b.equals(u))
return t ;
if(t.left != null)
t = t.left ;
else
t = t.right ;
}
return null ;
}
/**
* 为entry返回SimpleImutableEntry,如果为null,则返回null
* @param <K>
* @param <V>
* @param e
* @return
*/
private static <K,V> Map.Entry<K, V> exportEntry (Entry<K,V> e) {
return (e == null) ? null : new AbstractMap.SimpleImmutableEntry<>(e) ;
}
public Map.Entry<K, V> ceilingEntry (K key) {
return exportEntry( getCeilingEntry(key)) ;
}
public Object clone() {
TreeMap1<?,?> clone ;
try {
clone = (TreeMap1<?,?>)super.clone() ;
} catch ( CloneNotSupportedException e ) {
throw new InternalError(e) ;
}
return clone ;
}
/**
* 返回指定项的后续项,如果没有,则返回null。
* @param <K>
* @param <V>
* @param t
* 节点
* @return
*/
private static <K,V> Entry<K, V> successor (Entry<K,V> t) {
if( t == null )
return null ;
else if ( t.right != null ) {
Entry<K,V> p = t.right ;
while( p.left != null )
p = p.left ;
return p ;
}else {
Entry<K,V> p = t.parent ;
Entry<K,V> ch = t ;
while( p != null && ch == p.right ) {
ch = p ;
p = p.parent ;
}
return p ;
}
}
public abstract class PrivateEntry<T> implements Iterator<T>{
PrivateEntry(Entry<K,V> first) {
expectedModCount = modCount ;
lastReturned = null ;
next = first ;
}
Entry<K,V> next ;
Entry<K,V> lastReturned ;
int expectedModCount ;
public final boolean hasNext() {
return next != null ;
}
final Entry<K,V> nextEntry() {
Entry<K,V> e = next ;
if ( e == null)
throw new NoSuchElementException() ;
if ( modCount != expectedModCount)
throw new ConcurrentModificationException() ;
next = successor(e) ;
lastReturned = e ;
return e ;
}
}
public final class EntryIterator extends PrivateEntry<Map.Entry<K, V>>{
EntryIterator(Entry<K,V> first) {
super(first) ;
}
public Map.Entry<K, V> next () {
return nextEntry() ;
}
}
public class EntrySet extends AbstractSet<Map.Entry<K, V>>{
public Iterator<Map.Entry<K, V>> iterator() {
return new EntryIterator(getFirstEntry()) ;
}
public int size() {
return TreeMap1.this.size() ;
}
}
private transient EntrySet entrySet ;
/**
* 返回此映射中包含的映射的{@link Set}视图。
* @return
* 此映射中包含的映射的集合视图,按升序键排序
*/
public Set<Map.Entry<K, V>> entrySet() {
EntrySet es = entrySet ;
return( es != null) ? es : ( entrySet = new EntrySet()) ;
}
private void addEntry( K key , V value , Entry<K,V> parent , boolean addToLeft) {
Entry<K,V> e = new Entry<>(key, value, parent) ;
if (addToLeft)
parent.left = e ;
else
parent.right = e ;
size++ ;
modCount++ ;
}
public TreeMap1 () {
comparator = null ;
}
private static final boolean BLACK = true;
private static final boolean RED = false;
static final class Entry<K,V> implements Map.Entry<K,V>{
K key ; V value ; Entry<K,V> parent ;
Entry<K,V> left ; Entry<K,V> right ;
boolean color = BLACK;
Entry(K key , V value ,Entry<K,V> parent) {
this.key = key ;
this.value = value ;
this.parent = parent ;
}
public synchronized String toString() {
return key + " " + value ;
}
public K getKey() {
return key ;
}
public V getValue() {
return value ;
}
}
private final Comparator<? super K> comparator ;
/**
* 使用此树状图的正确比较方法比较两个键。
* @param k1
* @param k2
* @return
*/
@SuppressWarnings("unchecked")
private final int compare ( Object k1 , Object k2) {
return comparator == null ? ((Comparable<? super K>)k1).compareTo((K)k2) : comparator.compare((K)k1, (K)k2) ;
}
private transient int size = 0 ;
private transient int modCount ;
private void addEntry(K key , V value ) {
compare(key , key) ;
root = new Entry<>(key , value , null) ;
size++ ;
modCount++ ;
}
private transient Entry<K,V> root ;
public V put(K key , V value , boolean repla) {
Entry<K,V> t = root ;
if ( t == null ) {
addEntry(key , value) ;
return value ;
}
Entry<K,V> parent ; int cmp ;
Comparator<? super K> cpr = comparator ;
if ( cpr != null) {
Objects.nonNull(key) ;
do {
parent = t ;
cmp = cpr.compare(key , t.key) ;
if ( cmp < 0 )
t = t.left ;
else if ( cmp > 0 )
t = t.right ;
else {
V oldValue = t.value ;
if ( repla || oldValue == null)
t.value = value ;
return oldValue ;
}
} while( t != null ) ;
} else {
Objects.nonNull(key) ;
@SuppressWarnings("unchecked")
Comparable<? super K > k = (Comparable<? super K>) key ;
do {
parent = t ;
cmp = k.compareTo(t.key) ;
if ( cmp < 0 )
t = t.left ;
else if ( cmp > 0 )
t = t.right ;
else {
V oldValue = t.value ;
if ( repla || oldValue == null)
t.value = value ;
return oldValue ;
}
} while( t != null ) ;
}
addEntry(key , value , parent , cmp < 0 ) ;
return null ;
}
}
}
集合调用 :
TreeSet<Object> tre = new TreeSet<Object>() ;
tre.add("ksdafhkadjshfi0") ;
for(int u = 0 ; u < 10 ; u++) {
tre.add("jkdfhgkjdsfh"+ u) ;
}
tre.add("ksdafhkadfgfdgf4524jshfi0") ;
System.out.println("浅层副本:"+" "+tre.clone());
System.out.println("大于或等于给定元素的最小元素:"+" "+tre.ceiling("jkdfhgkjdsfh8"));
System.out.println("是否存在指定元素:"+" "+tre.contains("ksdafhkadfgfdgf4524jshfi0"));
System.out.println("第一个元素:"+" "+tre.first());
System.out.println("小于或等于e的最大元素:"+" " + tre.floor("jkdfhgkjdsfh8"));
System.out.println("大于e的最小元素:"+" "+tre.higher("jkdfhgkjdsfh6"));
System.out.println("是否包含键:"+" "+tre.isEmpty());
System.out.println("最后一个(最高)键:"+" "+tre.last());
System.out.println("小于给定的元素:"+" "+tre.lower("jkdfhgkjdsfh8"));
System.out.println("检索并删除第一个(最低)元素:"+" "+tre.pollFirst());
System.out.println("检索并删除最后一个(最高)元素:"+" "+tre.pollLast());
System.out.println("删除密钥:"+" "+tre.remove("jkdfhgkjdsfh7"));
System.out.println("元素范围:"+" "+tre.subSet("jkdfhgkjdsfh5","jkdfhgkjdsfh9"));
System.out.println("等于指定元素以及以上部分视图"+" "+tre.tailSet("jkdfhgkjdsfh3"));
System.out.println("一组数组"+" "+tre.toArray());
Object[] obj = tre.toArray() ;
for(Object ect : obj)
System.out.println(ect);
System.out.println("浅层副本:"+" "+tre.clone());
System.out.println("集合中的元素数:"+" "+tre.size());
集合方法的相关引用 有以下几种 :
集合调用 :
TreeSet<Object> tre = new TreeSet<Object>() ;
tre.add("ksdafhkadjshfi0") ;
for(int u = 0 ; u < 10 ; u++) {
tre.add("jkdfhgkjdsfh"+ u) ;
}
tre.add("ksdafhkadfgfdgf4524jshfi0") ;
System.out.println("浅层副本:"+" "+tre.clone());
System.out.println("大于或等于给定元素的最小元素:"+" "+tre.ceiling("jkdfhgkjdsfh8"));
System.out.println("是否存在指定元素:"+" "+tre.contains("ksdafhkadfgfdgf4524jshfi0"));
System.out.println("第一个元素:"+" "+tre.first());
System.out.println("小于或等于e的最大元素:"+" " + tre.floor("jkdfhgkjdsfh8"));
System.out.println("大于e的最小元素:"+" "+tre.higher("jkdfhgkjdsfh6"));
System.out.println("是否包含键:"+" "+tre.isEmpty());
System.out.println("最后一个(最高)键:"+" "+tre.last());
System.out.println("小于给定的元素:"+" "+tre.lower("jkdfhgkjdsfh8"));
System.out.println("检索并删除第一个(最低)元素:"+" "+tre.pollFirst());
System.out.println("检索并删除最后一个(最高)元素:"+" "+tre.pollLast());
System.out.println("删除密钥:"+" "+tre.remove("jkdfhgkjdsfh7"));
System.out.println("元素范围:"+" "+tre.subSet("jkdfhgkjdsfh5","jkdfhgkjdsfh9"));
System.out.println("等于指定元素以及以上部分视图"+" "+tre.tailSet("jkdfhgkjdsfh3"));
System.out.println("一组数组"+" "+tre.toArray());
Object[] obj = tre.toArray() ;
for(Object ect : obj)
System.out.println(ect);
System.out.println("浅层副本:"+" "+tre.clone());
System.out.println("集合中的元素数:"+" "+tre.size());