Class SortedList<T>

Type Parameters:
T - 列表类型.
All Implemented Interfaces:
Serializable, Iterable<T>, Collection<T>, List<T>

@PowerNukkitXOnly @Since("1.6.0.0-PNX") public class SortedList<T> extends AbstractList<T> implements Serializable
SortedList是List的一个有序实现,内部是用平衡二叉树实现的。

请注意,你不能对这个列表做指定项操作,除了remove(int)remove(Object)clearadd()

此列表操作的时间复杂度如下:containsaddremove以及get 的时间复杂度为O(log(n))

此列表不保证线程安全,若有必要,请使用Collections.synchronizedList(List)包装以确保线程安全。

这个列表提供的迭代器是快速失效的,所以除了通过迭代器本身之外的任何结构修改都会导致它抛出ConcurrentModificationException

See Also:
  • Constructor Details

    • SortedList

      public SortedList(Comparator<? super T> comparator)
      构造一个新的空SortedList,根据给定的比较器对元素进行排序。
      Parameters:
      comparator - 给元素排序的比较器
  • Method Details

    • add

      public boolean add(T object)
      将给定对象插入SortedList的适当位置,以确保列表中的元素按给定比较器指定的顺序保存。

      此方法仅允许添加非null值,如果给定对象为null,则列表保持不变并返回false。

      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface List<T>
      Overrides:
      add in class AbstractList<T>
      Parameters:
      object - 要添加的元素
      Returns:
      当给定对象为null时为false,否则为true
    • add

      protected void add(SortedList<T>.Node toAdd)
      将给定节点添加到此SortedList。

      此方法可以被子类重写,以便更改此列表将存储的节点的定义。

      此实现使用SortedList.Node.compareTo(Node)方法来确定给定节点应该存储在哪里。它还会增加此列表的modCount。

      Parameters:
      toAdd - 要新增的节点
    • iterator

      public Iterator<T> iterator()
      提供一个迭代器,该迭代器按照给定比较器确定的顺序提供此SortedList的元素。

      此迭代器实现允许以O(n)时间复杂度迭代整个列表,其中n是列表中的元素数。

      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface List<T>
      Overrides:
      iterator in class AbstractList<T>
      Returns:
      一个按照给定比较器确定的顺序提供这个分类列表的元素的迭代器。
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface List<T>
      Specified by:
      size in class AbstractCollection<T>
      Returns:
      存储在此SortedList中的元素数量。
    • getRoot

      protected SortedList<T>.Node getRoot()
      Returns:
      此SortedList的根节点,如果此列表为空,则为空。
    • contains

      public boolean contains(Object obj)
      返回给定对象是否在此SortedList中。

      元素比较使用Object.equals(Object)方法,并假设给定的obj必须具有与此SortedList中的元素相等的T类型。 时间复杂度为O(log(n)),其中n是列表中的元素数。

      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface List<T>
      Overrides:
      contains in class AbstractCollection<T>
      Parameters:
      obj - 要检查存在性的对象
      Returns:
      是否存在于此SortedList中
    • findFirstNodeWithValue

      protected SortedList<T>.Node findFirstNodeWithValue(T value)
      返回表示树中给定值的节点,如果不存在此类节点,则该节点可以为null。

      该方法使用给定的比较器执行二进制搜索,时间复杂度为O(log(n))。

      Parameters:
      value - 要搜索的值
      Returns:
      此列表中具有给定值的第一个节点
    • remove

      public T remove(int index)
      移除并返回此SortedList中给定索引处的元素。由于列表已排序,这是从0-n-1开始计数的第四个最小元素。

      例如,调用remove(0)将删除列表中最小的元素。

      Specified by:
      remove in interface List<T>
      Overrides:
      remove in class AbstractList<T>
      Parameters:
      index - 要删除的元素的索引
      Returns:
      被删除的元素
      Throws:
      IllegalArgumentException - 如果索引不是有效的索引则抛出此异常
    • remove

      public boolean remove(Object value)
      删除列表中具有给定值的第一个元素(如果存在这样的节点),否则不执行任何操作。使用给定的比较器对元素进行比较。

      返回是否找到并删除了匹配的元素。

      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface List<T>
      Overrides:
      remove in class AbstractCollection<T>
      Parameters:
      value - 要移除的元素
      Returns:
      是否找到并删除了匹配的元素。
    • remove

      protected void remove(SortedList<T>.Node toRemove)
      从这个SortedList中删除给定的节点,如果需要重新平衡,也会添加modCount。 时间复杂度O(log(n))。
      Parameters:
      toRemove - 此SortedList中的节点
    • get

      public T get(int index)
      返回此SortedList中给定索引处的元素。由于列表已排序,因此这是从0-n-1开始计算的第四个最小元素的索引。

      例如,调用get(0)将返回列表中最小的元素。

      Specified by:
      get in interface List<T>
      Specified by:
      get in class AbstractList<T>
      Parameters:
      index - 要获取的元素的索引
      Returns:
      此SortedList中给定索引处的元素
      Throws:
      IllegalArgumentException - 如果索引不是有效的索引则抛出此异常
    • findNodeAtIndex

      protected SortedList<T>.Node findNodeAtIndex(int index)
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface List<T>
      Overrides:
      isEmpty in class AbstractCollection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface List<T>
      Overrides:
      clear in class AbstractList<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
      Overrides:
      toArray in class AbstractCollection<T>
    • toArray

      public <E> E[] toArray(E[] holder)
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
      Overrides:
      toArray in class AbstractCollection<T>