当前位置: 首页 > news >正文

java常用集合方法

目录

    • 一、Iterator接口
    • 二、Iterable接口
    • 三、Collection接口
    • 四、Collection与Iterable关系

一、Iterator接口

Iterator 是一个集合迭代器接口,它提供了以下方法:

  1. 判断迭代器中是否还拥有元素,有则返回true,否则返回false

    boolean hasNext();

  2. 获取迭代器中的下一个元素

    E next();

  3. 移除集合中的元素
    default void remove() { throw new UnsupportedOperationException("remove"); }

  4. 对迭代器中剩余的元素进行操作(从 jdk 8 开始)

    default void forEachRemaining(Consumer<? super E> action) {
         Objects.requireNonNull(action);
         while (hasNext())
        action.accept(next());
    }

二、Iterable接口

Iterable是一个迭代接口

public interface Iterable<T> {/*** Returns an iterator over elements of type {@code T}.** @return an Iterator.*/Iterator<T> iterator();/*** Performs the given action for each element of the {@code Iterable}* until all elements have been processed or the action throws an* exception.  Unless otherwise specified by the implementing class,* actions are performed in the order of iteration (if an iteration order* is specified).  Exceptions thrown by the action are relayed to the* caller.** @implSpec* <p>The default implementation behaves as if:* <pre>{@code*     for (T t : this)*         action.accept(t);* }</pre>** @param action The action to be performed for each element* @throws NullPointerException if the specified action is null* @since 1.8*/default void forEach(Consumer<? super T> action) {Objects.requireNonNull(action);for (T t : this) {action.accept(t);}}/*** Creates a {@link Spliterator} over the elements described by this* {@code Iterable}.** @implSpec* The default implementation creates an* <em><a href="Spliterator.html#binding">early-binding</a></em>* spliterator from the iterable's {@code Iterator}.  The spliterator* inherits the <em>fail-fast</em> properties of the iterable's iterator.** @implNote* The default implementation should usually be overridden.  The* spliterator returned by the default implementation has poor splitting* capabilities, is unsized, and does not report any spliterator* characteristics. Implementing classes can nearly always provide a* better implementation.** @return a {@code Spliterator} over the elements described by this* {@code Iterable}.* @since 1.8*/default Spliterator<T> spliterator() {return Spliterators.spliteratorUnknownSize(iterator(), 0);}
}

从上面的源码可以看出,Iterable是一个接口,它提供了 以下三个方法:

  1. 返回一个可迭代对象

    Iterator<T> iterator();

  2. forEach 遍历(从jdk8开始)

    default void forEach(Consumer<? super T> action) {
         Objects.requireNonNull(action);
              for (T t : this) {
                   action.accept(t);
              }
    }

  3. 返回一个用于遍历集合的 Spliterator。这个方法是Java 8引入的,它允许集合实现者提供一个高效的遍历器,而不需要像以前那样实现 Iterator 接口。

    default Spliterator<T> spliterator() {
         return Spliterators.spliteratorUnknownSize(iterator(), 0);
    }

    这里,Spliterators.spliterator 是一个静态方法,它接受一个集合和一个特征集合作为参数。特征集合是一个位掩码,用于指定 Spliterator 的特性,比如是否有序、是否并行等。在默认实现中,特征集合是 0,表示没有任何特性。

    如果你正在实现自己的集合类,并且想要提供一个更高效的 Spliterator,你可以覆盖这个方法,并提供你自己的实现。

三、Collection接口

Collection是一个集合接口,除了默认实现方法外,定义了集合操作的常用方法,如:

  1. 集合元素数量

    int size();

  2. 判断集合是否为空

    boolean isEmpty();

  3. 判断集合是否包含某个元素

    boolean contains(Object o);

  4. 返回一个迭代器

    Iterator<E> iterator();

  5. 返回包含此集合中所有元素的数组

    Object[] toArray();

  6. 添加元素

    boolean add(E e);

  7. 移除元素

    boolean remove(Object o);

  8. 判断集合是否包含指定集合中的所有元素

    boolean containsAll(Collection<?> c);

  9. 添加指定集合的所有元素

    boolean addAll(Collection<? extends E> c);

  10. 移除指定集合的所有元素

    boolean removeAll(Collection<?> c);

  11. 删除此集合中满足给定条件的所有元素(jdk8开始支持)

    default boolean removeIf(Predicate<? super E> filter) {
         Objects.requireNonNull(filter);
         boolean removed = false;
         final Iterator<E> each = iterator();
         while (each.hasNext()) {
              if (filter.test(each.next())) {
                   each.remove();
                   removed = true;
              }
         }
         return removed;
    }

  12. 仅保留此集合中包含在指定集合中的元素(可选操作)。换句话说,从此集合中删除未包含在指定集合中的所有元素。

    boolean retainAll(Collection<?> c);

  13. 从此集合中删除所有元素。此方法返回后,集合将为空。

    void clear();

  14. 返回一个用于遍历集合的 Spliterator(从jdk8开始支持)

    @Override
    default Spliterator<E> spliterator() {
         return Spliterators.spliterator(this, 0);
    }

  15. 返回一个Stream流(从jdk8开始支持)

    default Stream<E> stream() {
    return StreamSupport.stream(spliterator(), false);
    }

四、Collection与Iterable关系

    
Collection接口继承了Iterable接口,复用了Iterable中的相关方法。
public interface Collection<E> extends Iterable<E> {

在这里插入图片描述


http://www.mrgr.cn/news/21931.html

相关文章:

  • 【Java并发编程一】八千字详解多线程
  • Kotlin 枚举类
  • 用Python实现时间序列模型实战——Day 14: 向量自回归模型 (VAR) 与向量误差修正模型 (VECM)
  • ChatGPT+数学建模:快速搞定2024国赛A题“板凳龙”!
  • Request Response
  • 【C++】栈和队列、优先级队列、适配器原理
  • 游戏厅计费系统电玩计时系统软件 佳易王计时定时语音提醒管理系统操作教程
  • 在RK3588开发板上部署AI
  • 【C++二分查找】2439. 最小化数组中的最大值
  • H5漂流瓶社交系统源码
  • 如何打造个性化大学生线上聊天交友系统?Java SpringBoot Vue教程,2025最新设计思路
  • 小程序微信支付API?以及参数有哪些
  • 力扣最热一百题——矩阵置零
  • Maven快速上手使用指南的笔记
  • 使用Node-API进行线程安全开发
  • 用Python编写倒计时程序:详细教程
  • “阡陌云旅”黄河九省文化旅游平台
  • Vue——Diff算法
  • Wni11 下 WSL 安装 CentOS
  • 使用WebP解决网站加载速度问题,这些细节你需要了解