Java 集合框架(Collection 接口)

2018-11-12  本文已影响0人  6ea566508d0d

Collection 接口简介

对于 Collection 接口,Java的官方文档这样提到:

A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument. This constructor, known as a conversion constructor, initializes the new collection to contain all of the elements in the specified collection, whatever the given collection's subinterface or implementation type. In other words, it allows you to convert the collection's type.

可以简单理解为:


Collection 接口相关操作

Collection 接口作为集合层次结构的根接口,自然抽象了很多公共操作的方法。
根据 Java 官方文档,这些操作被分为:
    基础操作批量操作数组操作、以及 JDK1.8 后提供的聚合操作

int size() // 返回集合中元素的个数
boolean isEmpty() // 判断集合是否存在元素
boolean contains(Object element) // 判断集合是否包含
boolean add(E element) // 添加元素
boolean remove(Object element) // 删除元素
Iterator<E> iterator() // 获取该集合的迭代器
boolean containsAll(Collection<?> c) // 包含
boolean addAll(Collection<? extends E> c) //并集
boolean removeAll(Collection<?> c) // 差集
boolean retainAll(Collection<?> c) // 交集
void clear() // 清空
Object[] toArray() // 集合转换为数组
<T> T[] toArray(T[] a)  // 集合转换为指定类型的数组

关于遍历

Java 官方文档中提到了三种遍历集合的方式:
    (1)使用聚合操作、(2)使用for-each构造、(3)使用迭代器。

for (Object o : collection) {
    System.out.println(o);
} 
static void filter(Collection<?> c) {
    for (Iterator<?> it = c.iterator(); it.hasNext(); )
        if (!cond(it.next()))
            it.remove();
}

最后简单说一下,关于迭代器,
以下是迭代器的接口:

public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove(); //optional
}

迭代器设计的也非常简单粗暴:
hasNext - 判断集合中是否还有下一个元素
next - 获取集合中的下一个元素
remove - 删除当前迭代的元素(这是一个可选操作,即实现类可以选择不实现这个方法)

这里还需要说一点的是:

ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
        
for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {

    Integer next = it.next();
    if (next == 7) {
        it.remove(); // 不会抛出异常
    }

    list.set(4, 7); // 不会抛出异常
    list.add(8); // 抛出异常 ConcurrentModificationException
    list.remove(6); // 抛出异常 ConcurrentModificationException
}

原因嘛,有很多细讲这方面的文章,这里简单提一下:
迭代器的模型,有点像指针一样,调用next() 方法则移动到下一个元素,若,list.add(0, 7) 在集合头部插入一个元素,迭代器则很有可能永远迭代不到这个元素

简单理解为:
迭代过程中,迭代行为由迭代器控制,故任何改变集合结构的行为也应由迭代器控制,即:


更多参考:The Collection Interface (The Java™ Tutorials > Collections > Interfaces)

上一篇 下一篇

猜你喜欢

热点阅读