java集合框架01——Collection源码学习

 

1.Collection接口

Collection是List、Set等集合高度抽象出来的接口,它包含了这些集合的基本操作,主要分为List和Set。

List和Set都是接口,它们都继承Collection。

List是有序放入,可以有重复的元素。

Set是无序放入的,不可以有重复元素注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的

java源码中,抽象出来了AbstractCollection类让Collection继承,该类实现了Collection中的大部分方法,AbstractList和AbstractSet都继承与AbstractCollection,具体的List实现类继承与AbstractList,而Set的实现类则继承与AbstractSet。Collection中有个iterator()方法,它的作用是返回一个Iterator接口。通常,我们通过Iterator迭代器来遍历集合。ListIterator是List接口所特有的,在List接口中,通过ListIterator()返回一个ListIterator对象。

List接口有三个实现类:LinkedList,ArrayList,Vector ,Set接口有两个实现类:HashSet(底层由HashMap实现),LinkedHashSet

接口的设计图,如下:

Collection定义

直接看源码和里面的api:

public interface Collection<E> extends Iterable<E> {
    int size();
    boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator();
    Object[] toArray();
    <T> T[] toArray(T[] a);
    boolean add(E e);
    boolean remove(Object o);
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean removeAll(Collection<?> c);
    boolean retainAll(Collection<?> c);
    void clear();
    boolean equals(Object o);
    int hashCode();
}

从上面的源码中可以看出Collection是一个接口,接口中包括了15中api。Collection是一个高度集中的集合,集合中包含增加,删除,清空,遍历集合,是否为空,集合的长度等等。

List的定义

直接看源码和里面的api,List继承了Collection接口,并且包含了Collection中所有的接口,其中也增加了自己的接口。

public interface List<E> extends Collection<E> {
    //Collection中继承的api
    int size();
    boolean isEmpty();
    boolean contains(Object o);  
    Iterator<E> iterator();
    Object[] toArray();
    <T> T[] toArray(T[] a);
    boolean add(E e);
    boolean remove(Object o);
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);    
    boolean removeAll(Collection<?> c);
    boolean retainAll(Collection<?> c);
    void clear();
    boolean equals(Object o);
    int hashCode();
    //list新增的api
    boolean addAll(int index, Collection<? extends E> c);//在指定位置添加其它集合的元素
    E get(int index);//获取指定位置的元素
    E set(int index, E element);//修改指定位置的元素
    void add(int index, E element);//在指定位置添加元素
    E remove(int index);//移除指定位置的元素
    int indexOf(Object o);//获取指定位置的索引
    int lastIndexOf(Object o);//获取最后位置的索引
    ListIterator<E> listIterator(); //获取迭代器
    ListIterator<E> listIterator(int index); 
    List<E> subList(int fromIndex, int toIndex); //截取指定位置的list元素
}

Set的定义

Set也是继承Collection接口,且Set不能有重复元素。Set里接口Api与Collection接口中一样,没有添加新的接口api。在看下:


public interface Set<E> extends Collection<E> {
    int size();
    boolean isEmpty();
    boolean contains(Object o);
    Object[] toArray();
    <T> T[] toArray(T[] a);
	Iterator<E> iterator();
    boolean add(E e);
    boolean remove(Object o);
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean retainAll(Collection<?> c);
    boolean removeAll(Collection<?> c);
    void clear();
    boolean equals(Object o);
    int hashCode();
}

对于抽象类AbstractCollection实现了Collection接口,它实现了Collection中除了iterator()和size()之外的所有方法。AbstractCollection的主要作用是方便其他类实现Collection.,比如ArrayList、LinkedList等。它们想要实现Collection接口,通过集成AbstractCollection就已经实现大部分方法了,再实现一下iterator()和size()即可。看一下类的定义

public abstract class AbstractCollection<E> implements Collection<E> {}

具体内部的源码没有深扒,只是简单的看了下。有时间在细细的看一下。

AbstractList的定义

abstractList是一个继承AbstractCollection,并且实现了List接口的抽象类。它实现了List中除了size()、get(int location)之外的方法。bstractList的主要作用:它实现了List接口中的大部分函数,从而方便其它类继承List。另外,和AbstractCollection相比,AbstractList抽象类中,实现了iterator()方法。对于创建ArrayList做准备。看一下类的定义(类中的方法没有深扒):

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}

 AbstractSet的定义

AbstractSet也是是一个继承与AbstractCollection,并且实现了Set接口的抽象类。由于Set接口和Collection接口中的API完全一样,所以Set也就没有自己单独的API。和AbstractCollection一样,它实现了List中除iterator()和size()外的方法。所以源码和AbstractCollection的一样。AbstractSet的主要作用:它实现了Set接口总的大部分函数,从而方便其他类实现Set接口。

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}

Iterator的定义

Iterator是一个接口,它是集合的迭代器。集合可以通过Iterator去遍历其中的元素。Iterator提供的API接口包括:是否存在下一个元素,获取下一个元素和删除当前元素。看一下接口定义:

public interface Iterator<E> {
    boolean hasNext(); //判断是否有下一个元素
    E next(); //下一个元素
    void remove(); //移除元素
}

ListIterator继承了Iterator接口,它是队列迭代器。专门用于遍历List,能提供向前和向后遍历。相比于Iterator,它新增了添加、是否存在上一个元素、获取上一个元素等API接口。看一下接口的定义及接口api:

public interface ListIterator<E> extends Iterator<E> {
    boolean hasNext();
    E next();
	void remove();
	//新增的api
    boolean hasPrevious();  //判断是否有以前的元素
    E previous();  //获取以前的元素
    int nextIndex(); //获取下一个索引
    int previousIndex(); //获取前一个索引
    void set(E e); //编辑元素
    void add(E e); //增加新元素
}

对于Collection的学习先到这里,有时间会在继续的研究一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值