栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java集合框架、 Collection、ArrayList详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合框架、 Collection、ArrayList详解

1. Collection < E >

集合框架结构中的根接口。

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否则falseboolean addAll(Collection c); 传入实现了Collection接口的集合并且其泛型类型为该集合泛型类型或者子类类型的、添加到当前集合中。boolean removeAll(Collection c); 将传入集合与当前集合取差集

default boolean removeIf(Predicate filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator 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 spliterator() {
        return Spliterators.spliterator(this, 0);
    }

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

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

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

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

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

2. ArrayList< E >

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

2.1 abstract class AbstractList < E >

2.1.1 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

构造器指定索引位置

 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();
            }
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735503.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号