Java集合框架、 Collection、ArrayList详解

1. Collection < E >

集合框架结构中的根接口。
Collection接口的所有方法

  • int size(); 返回集合中元素个数
  • boolean isEmpty(); 返回集合是否为空
  • boolean contains(Object o); 传入一个对象判断集合中是否存在
  • Iterator iterator(); 覆写父接口interface Iterable中迭代方法
  • Object[] toArray(); 返回集合为对象数组
  • T[] toArray(T[] a); 传入一个泛型数组

返回一个包含此集合中所有元素的数组;
返回的数组的运行时类型是指定数组的运行时类型。
如果集合适合指定的数组,则返回该集合。
类的运行时类型将为新数组分配
指定的数组和集合的大小。

  • boolean add(E e); 添加泛型元素到集合中
  • boolean remove(Object o); 删除集合中指定对象
  • boolean containsAll(Collection<?> c); 传入实现了Collection接口的集合并判断该集合是否与其有交集,有则返回true否则false
  • boolean addAll(Collection<? extends E> c); 传入实现了Collection接口的集合并且其泛型类型为该集合泛型类型或者子类类型的、添加到当前集合中。
  • boolean removeAll(Collection<?> c); 将传入集合与当前集合取差集
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;
    }

删除此集合中满足给定条件的所有元素

  • boolean retainAll(Collection<?> c); c集合作为白名单到该集合中取出名单中元素
  • void clear(); 从集合中移除所有元素
  • boolean equals(Object o); 将指定对象与此集合进行相等比较
  • int hashCode(); 返回此对象的hash值
 default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }

在此集合中的元素上创建一个扫描竞技器(用于遍历和分区源元素的对象)。

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

使用此集合作为其源返回顺序流。

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

将此集合作为其源返回可能并行流。
该方法允许返回顺序流。

2. ArrayList< E >

该类继承了AbstractList < E >抽像类、实现了List < E >, RandomAccess, Cloneable, java.io.Serializable接口

2.1 abstract class AbstractList < E >

AbstractList源文件中整体的内部结构

2.1.1 AbstractList

== AbstractList 抽象类结构

1. 内部类 Itr

在这里插入图片描述

  • boolean hasNext() 获取下一个元素是否存在、判断索引是否不等于size()
 public boolean hasNext() {
            return cursor != size();
        }

  • E next()获取下一个元素
 public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
  • void remove() 清空集合
   public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }
  • final void checkForComodification()
   final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
2. 内部类 ListItr

结构

private class ListItr extends Itr 
                     implements ListIterator<E>

ListItr 内部类的全部方法

  • 构造器指定索引位置
 ListItr(int index) {
            cursor = index;
        }
  • public boolean hasPrevious() 判断索引不为0
      public boolean hasPrevious() {
            return cursor != 0;
        }
  • public E previous() 返回一个元素
    public E previous() {
            checkForComodification();
            try {
                int i = cursor - 1;
                E previous = get(i);
                lastRet = cursor = i;
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
  • public int nextIndex() 下一个元素索引
 public int nextIndex() {
            return cursor;
        }
  • public int previousIndex() 前一个元素索引
   public int previousIndex() {
            return cursor-1;
        }
  • public void set(E e) 后一元素设置为e
 public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.set(lastRet, e);
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
  • public void add(E e) 追加元素
public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                AbstractList.this.add(i, e);
                lastRet = -1;
                cursor = i + 1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李同学va

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值