集合类

 

java数据结构2--集合总论

0.1、为什么出现集合类?

面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式(容器)
Java中集合也是类,真正用来存储东西的是某种集合类的实例对象。

java数据结构2--集合总论

0.2、集合类 VS 数组

数组和集合类都是容器,有何不同?集合类的特点

java数据结构2--集合总论

0.3、Collection接口概述

Collection 层次结构中的根接口。
Collection 表示一组对象,这些对象也称为 collection 的元素。
一些 collection 允许有重复的元素,而另一些则不允许。

java数据结构2--集合总论

 

 

java数据结构2--集合总论

java数据结构2--集合总论

 

0.4接口中的方法

添加操作
boolean add(Object e)
boolean addAll(Collection c)

删除操作
boolean remove(Object o)
boolean removeAll(Collection c)

查询操作
int size()

判断操作
boolean isEmpty()
boolean contains(Object obj)
boolean containsAll(Collection c)

java数据结构2--集合总论

java数据结构2--集合总论

0.5、继承父类的方法--迭代器

java数据结构2--集合总论

Iterator iterator()

说明:

  1. 迭代器不保证取出元素的顺序和存入的顺序一致,“有序”是靠集合实例本身保证的。
  2. 迭代器本身是一个接口,该方法返回的是一个迭代器实例对象,通常使用的是接口多态使用迭代器
  3. 迭代器中常用的两个方法是:

boolean hasNext():判断是否有下一个元素
Object next():取出下一个元素

 

 对ArrayList的方法进行分析

java数据结构2--集合总论

 

    /**
     * Returns an iterator over the elements in this list in proper sequence.
     *
     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
     *
     * @return an iterator over the elements in this list in proper sequence
     */
    public Iterator<E> iterator() {
        return new Itr();
    }

    /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer<? super E> consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
Iterator

相关文章: