Class ObservableList<E>

Type Parameters:
E - 元素类型
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess, SequencedCollection<E>

public class ObservableList<E> extends ArrayList<E>
可观察列表,支持监听元素新增和删除事件
Since:
2024-09-01 17:51
Author:
夜雨
See Also:
  • Constructor Details

    • ObservableList

      public ObservableList()
      创建空的可观察列表
    • ObservableList

      public ObservableList(Collection<? extends E> c)
      使用指定集合创建可观察列表
      Parameters:
      c - 初始化集合
  • Method Details

    • addChangeListener

      public void addChangeListener(CollectionChangeListener<E> listener)
      添加集合变更监听器
      Parameters:
      listener - 集合变更监听器
    • removeChangeListener

      public void removeChangeListener(CollectionChangeListener<E> listener)
      移除集合变更监听器
      Parameters:
      listener - 集合变更监听器
    • add

      public boolean add(E e)
      新增元素并通知监听器
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      e - 待新增元素
      Returns:
      true 表示列表发生变化
    • add

      public void add(int index, E element)
      在指定下标新增元素并通知监听器
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      index - 插入下标
      element - 待新增元素
    • addAll

      public boolean addAll(Collection<? extends E> c)
      批量新增元素并逐个通知监听器
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      c - 待新增元素集合
      Returns:
      true 表示列表发生变化
    • addAll

      public boolean addAll(int index, Collection<? extends E> c)
      从指定下标开始批量新增元素并逐个通知监听器
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      index - 起始下标
      c - 待新增元素集合
      Returns:
      true 表示列表发生变化
    • remove

      public E remove(int index)
      删除指定下标元素并通知监听器
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class ArrayList<E>
      Parameters:
      index - 待删除元素下标
      Returns:
      被删除元素
    • remove

      public boolean remove(Object o)
      删除指定元素并通知监听器
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class ArrayList<E>
      Parameters:
      o - 待删除元素
      Returns:
      true 表示删除成功
    • removeAll

      public boolean removeAll(Collection<?> c)
      批量删除元素并逐个通知监听器
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface List<E>
      Overrides:
      removeAll in class ArrayList<E>
      Parameters:
      c - 待删除元素集合
      Returns:
      true 表示列表发生变化
    • removeIf

      public boolean removeIf(Predicate<? super E> filter)
      按条件删除元素
      Specified by:
      removeIf in interface Collection<E>
      Overrides:
      removeIf in class ArrayList<E>
      Parameters:
      filter - 删除条件
      Returns:
      true 表示至少删除一个元素
    • subList

      public ObservableList<E> subList(int fromIndex, int toIndex)
      获取子列表的可观察副本
      Specified by:
      subList in interface List<E>
      Overrides:
      subList in class ArrayList<E>
      Parameters:
      fromIndex - 起始下标(含)
      toIndex - 结束下标(不含)
      Returns:
      子列表副本