Java集合汇总(一)

java中,提供的集合关系:

首先感觉这张图,开头有点问题,如果Collection发出的虚线箭头是实现关系那么,不应该是Iterator,而应该是Iterable。


Collection类

Collection是集合类最基本的接口,List、Set都是实现Collection接口。

Collection中方法:

	public interface Collection<E> extends Iterable<E> {

	    /**
	     * 返回当前集合中的元素个数
	     */
	    int size();
	    
	    /**
	     *判断当前集合是否为空 
	     */
	    boolean isEmpty();

	    /**
	     * 判断集合是否包含特定元素
	     */
	    boolean contains(Object o);

	    /**
	     * 返回用于访问集合每个元素的迭代器
	     */
	    Iterator<E> iterator();

	    /**
	     * 将集合转换为数组
	     */
	    Object[] toArray();

	    /**
	     * 返回这个集合的对象数组,如果a足够大,则将集合中元素全部放入a中,
	     * 否则重新分配一个数组。
	     */
	    <T> T[] toArray(T[] a);

	    // Modification Operations

	    /**
	     * 向集合中添加一个元素
	     */
	    boolean add(E e);

	    /**
	     * 从集合中删除特定的元素
	     */
	    boolean remove(Object o);

	    /**
	     * 判断当前集合是否包含c集合中的所有元素
	     */
	    boolean containsAll(Collection<?> c);

	    /**
	     * 将集合c中的全部元素添加到当前集合
	     */
	    boolean addAll(Collection<? extends E> c);

	    /**
	     * 将当前集合中包含集合c中的元素全部删除
	     */
	    boolean removeAll(Collection<?> c);

	    /**
	     * 从当前集合删除所有不在集合c中的元素
	     */
	    boolean retainAll(Collection<?> c);

	    /**
	     * 删除当前集合中的所有元素
	     */
	    void clear();


	    // Comparison and hashing

	    /**
	     * 比较指定对象与当期集合是否相等
	     */
	    boolean equals(Object o);

	    /**
	     * Returns the hash code value for this collection.
	     */
	    int hashCode();
	}

List列表

他是一个有序,可重复对数组的扩展。用户可以通过索引位置,查看元素。List作为列表接口通常不直接使用,而是使用他的实现类LinkList、ArrayList、Vector。其中ArrayList和Vector是基于数组进行存储的,LinkedList是基于链表存储的。Vector是线程安全的,LinkList和ArrayList不是线程安全的。

public interface List<E> extends Collection<E> {
    /**
     * 返回列表中的长度
     */
    int size();
    /**
     * 判断列表是否为空
     */
    boolean isEmpty();
    /**
     * 判断列表是否包含指定的对象
     */
    boolean contains(Object o);
    /**
     * 将列表转换为Iterator迭代器
     */
    Iterator<E> iterator();
    /**
     * 将列表转换为数组
     */
    Object[] toArray();
    /**
     * 将列表转换为数组,如果a足够大,可以将其全部元素装入。否则创建新的对象
     */
    <T> T[] toArray(T[] a);
    /**
     * 向列表中添加元素
     */
    boolean add(E e);
    /**
     * 从列表中移出元素
     */
    boolean remove(Object o);
    /**
     * 判断列表中是否包含c集合中的所有元素
     */
    boolean containsAll(Collection<?> c);
    /**
     * 将集合内c的元素全部添加到列表中
     */
    boolean addAll(Collection<? extends E> c);
    /**
     * 从列表index位置,将集合c中所有元素全部添加
     */
    boolean addAll(int index, Collection<? extends E> c);
    /**
     * 从列表中删除集合c中的元素
     */
    boolean removeAll(Collection<?> c);
    /**
     * 从当前列表中删除不在c集合中的元素
     */
    boolean retainAll(Collection<?> c);
    /**
     * 清除列表中所有元素
     */
    void clear();
    /**
     * 判断列表与指定对象是否相等
     */
    boolean equals(Object o);
    /**
     * 计算列表的哈希码
     */
    int hashCode();
    /**
     * 根据位置index获取列表中元素
     */
    E get(int index);
    /**
     * 将位置index元素,修改未element
     */
    E set(int index, E element);
    /**
     * 在index位置添加元素
     */
    void add(int index, E element);
    /**
     * 删除指定位置的元素
     */
    E remove(int index);
    /**
     * 判断元素o在列表中的位置
     */
    int indexOf(Object o);
    /**
     * 反方向查找元素o
     */
    int lastIndexOf(Object o);
    /**
     * 转换为可迭代的ListIterator
     */
    ListIterator<E> listIterator();
    /**
     * 同上,参数为第一次调用next()返回指定位置的元素
     */
    ListIterator<E> listIterator(int index);
    /**
     * 截取列表中一段
     */
    List<E> subList(int fromIndex, int toIndex);
}

集合中为List集合提供了ListIterator,该类实现了Iterator接口,可以访问前一个元素和后一个元素

	public interface ListIterator<E> extends Iterator<E> {
	    /**
	     * 判断是否还有下一个元素
	     */
	    boolean hasNext();
	    /**
	     * 获取下一个元素
	     */
	    E next();
	    /**
	     * 判断是否还有前一个元素
	     */
	    boolean hasPrevious();
	    /**
	     * 获取前一个元素
	     */
	    E previous();
	    /**
	     * 获取下一个元素位置
	     */
	    int nextIndex();
	    /**
	     * 获取前一个元素位置
	     */
	    int previousIndex();
	    /**
	     * 删除迭代器中元素
	     */
	    void remove();
	    /**
	     * 用新元素修改next或privious上次访问的元素。如果没有元素则抛出异常
	     */
	    void set(E e);
	    /**
	     * 在当前位置添加一个元素
	     */
	    void add(E e);
	}

LikeList链表

链表的优缺点:添加删除操作比ArrayList高效,但是在随机访问方面就差很多了,这也是与ArrayList最大的区别。当确定使用链表时,应确保不会大量使用随机访问,而是使用经常使用插入,删除操作。java中的链表都是双向链接的,即每个节点都存储着指向前驱和后集的引用。注意如果在LinkList中使用get(n)这个方法获取元素,他的效率很低,因为调用一次都是从头遍历过来。当在链表中如果使用这个方法的时候,就有可能选错数据结构了。链表中访问元素,使用迭代器。

booleanadd(E e)
在列表末尾添加元素
voidadd(int index,E element)
在列表指定位置添加元素
booleanaddAll(Collection<? extends E> c)
在链表之后添加集合元素,其实具体实现是调用的add(int index,Collection<? entends E> c),传过去的index就是当期列表长度
booleanaddAll(int index,Collection<? extends E> c)
在链表指定位置添加集合元素
voidaddFirst(E e)
在链表头添加元素
voidaddLast(E e)
在链表之后添加元素
voidclear()
清除链表中的所有元素
Objectclone()
浅拷贝链表中的元素
booleancontains(Object o)
判断列表中是否存在指定元素,其实源码是调用indexOf()判读位置实现的
Iterator<E>descendingIterator()
返回链表的迭代器
Eelement()
获取当前列表元素,但不移出
Eget(int index)
获取指定位置的链表元素
EgetFirst()
获取链表表头元素
EgetLast()
获取链表表尾元素
intindexOf(Object o)
返回指定元素在链表中的指定位置
intlastIndexOf(Object o)
返回链表中指定元素最后出现的位置
ListIterator<E>listIterator(int index)
返回listIterator迭代器,支持向前访问
booleanoffer(E e)
在链表尾部添加元素,内部实现其实调用的是add()方法
booleanofferFirst(E e)
在链表头添加元素,内部实现就是调用addFirst();
booleanofferLast(E e)
在链表尾部添加元素,内部实现就是调用addLast();
Epeek()
返回表头元素,但不移出元素
EpeekFirst()
返回表头元素,但不移出元素
EpeekLast()
返回表位元素,但不移出表位元素
Epoll()
返回表头元素,并且删除表头元素
EpollFirst()
返回表头元素,并且删除表头元素
EpollLast()
返回表位元素,并且删除表尾元素
Epop()
弹出链表中的元素
voidpush(E e)
向链表中压入元素
Eremove()
删除表头元素并且返回
Eremove(int index)
删除指定位置的元素,并且返回该位置的元素
booleanremove(Object o)
删除第一次出现该元素的节点
EremoveFirst()
删除表头元素,并且返回该元素
booleanremoveFirstOccurrence(Object o)
从表头开始遍历到表尾,删除该元素第一次出现的节点
EremoveLast()
删除并且返回链表最后一个元素
booleanremoveLastOccurrence(Object o)
删除指定元素在该链表最后一次出现的位置
Eset(int index,E element)
将指定位置的元素修改为指定元素
intsize()
返回链表长度
Object[]toArray()
将链表转换为数组,其类型是Object类型
<T> T[]toArray(T[] a)
将链表转换为数组,类型是元素类型


在看链表源码的时候,可以通过看看Node类的封装,可以看出其存储了前一个节点及后一个节点和节点元素:

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }



ArrayList数组列表

ArrayList是基于数组实现的,是一个动态数组,容量可以自增长。但他不是线程安全的,只能在单线程下运行,多线程环境下可以考虑Collections.synchronizedList(List l)返回一个线程安全的数组。

public class ArrayList<E> extends AbstractList<E>  implements List<E>, RandomAccess, Cloneable, java.io.Serializable
实现Serializable接口,因此支持序列化,能够通过序列化远程传输。实现Cloneable接口,因此支持克隆。实现RandonAccess,所以支持随机访问。


ArrayList源码实现:

    private transient Object[] elementData;
    private int size;

elementData是存储在数组列表中的元素,size是数组中的元素个数。transient是在序列化机制中,可能有些元素不想被序列化,使用该关键词,可以去除序列化。关于序列化可以看我另一篇博客http://blog.sina.com.cn/s/blog_a36d34240102vkbb.html


public ArrayList(int size);
public ArrayList();
public ArrayList(Collection <? extends E> c);
ArrayList提供了三种构造方法,第一个指定特定长度,第二个是构造一个空的长度为0的数组列表,第三个是构造一个含有指定collection元素的数组列表


提供的方法:

booleanadd(E e)
在列表末尾添加元素
voidadd(int index,E element)
在列表指定位置添加元素
booleanaddAll(Collection<? extends E> c)
将集合c中全部元素添加到列表末尾
booleanaddAll(int index,Collection<? extends E> c)
向列表指定位置添加集合元素
voidclear()
删除数组列表中所有元素
Objectclone()
克隆当前列表数组
booleancontains(Object o)
判断当前元素是否包含在该列表中
voidensureCapacity(int minCapacity)
调整数组容量
Eget(int index)
获取指定位置的元素
intindexOf(Object o)
获取指定对象的索引位置,如果该列表不存在该元素,则返回-1
booleanisEmpty()
判断该列表是否为空
Iterator<E>iterator()
返回该列表的迭代器
intlastIndexOf(Object o)
查找最后一个是指定对象o的索引位置,如果不存在,则返回-1
ListIterator<E>listIterator()
返回listIterator迭代器
ListIterator<E>listIterator(int index)
返回listIterator迭代器,下一个元素是指定位置的元素
Eremove(int index)
删除指定位置的元素
booleanremove(Object o)
删除指定对象
booleanremoveAll(Collection<?> c)
从列表中删除包含集合c中的所有元素
protected voidremoveRange(int fromIndex, int toIndex)
删除指定区间的元素
booleanretainAll(Collection<?> c)
删除不包含在集合c中的元素
Eset(int index,E element)
将指定位置的元素替换为element
intsize()
返回数组列表长度
List<E>subList(int fromIndex, int toIndex)
截取列表中指定位置的元素
Object[]toArray()
将列表转换为数组
<T> T[]toArray(T[] a)
将列表转换为数组,用a进行存储,如果存储不下则新建数组
voidtrimToSize()
调整底层数组,为当前长度的数组

最好接合源码进行查看可以查看http://www.cnblogs.com/ITtangtang/p/3948555.html、http://blog.youkuaiyun.com/jzhf2012/article/details/8540410写的不错


Vector

Vector的随机访问速度会比ArrayList慢,但是Vector是线程安全的,所以当需要多线程环境下还要考虑Vector。Vector中的方法使用synchronized修饰

他也是实现了可增长的数组,维持着capacityIncrement变量,capacityIncrement为每次增长的容量。整体实现原理与ArrayList一样,但是全程是线程安全的

  /**
   *initalCapacity为初始化数组长度,capacityIncrement为自动增长的倍数
   */ 
   public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

    /**
     */
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

    /**
     * 无参构造方法,默认长度是10
     */
    public Vector() {
        this(10);
    }

    /**
     * 将集合元素初始化到Vector中
     */
    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

booleanadd(E e)
Appends the specified element to the end of this Vector.
voidadd(int index, E element)
Inserts the specified element at the specified position in this Vector.
booleanaddAll(Collection<? extends E> c)
Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.
booleanaddAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified Collection into this Vector at the specified position.
voidaddElement(E obj)
Adds the specified component to the end of this vector, increasing its size by one.
intcapacity()
Returns the current capacity of this vector.
voidclear()
Removes all of the elements from this Vector.
Objectclone()
Returns a clone of this vector.
booleancontains(Object o)
Returns true if this vector contains the specified element.
booleancontainsAll(Collection<?> c)
Returns true if this Vector contains all of the elements in the specified Collection.
voidcopyInto(Object[] anArray)
Copies the components of this vector into the specified array.
EelementAt(int index)
Returns the component at the specified index.
Enumeration<E>elements()
Returns an enumeration of the components of this vector.
voidensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
booleanequals(Object o)
Compares the specified Object with this Vector for equality.
EfirstElement()
Returns the first component (the item at index 0) of this vector.
Eget(int index)
Returns the element at the specified position in this Vector.
inthashCode()
Returns the hash code value for this Vector.
intindexOf(Object o)
Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
intindexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
voidinsertElementAt(E obj, int index)
Inserts the specified object as a component in this vector at the specified index.
booleanisEmpty()
Tests if this vector has no components.
Iterator<E>iterator()
Returns an iterator over the elements in this list in proper sequence.
ElastElement()
Returns the last component of the vector.
intlastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
intlastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.
ListIterator<E>listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E>listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Eremove(int index)
Removes the element at the specified position in this Vector.
booleanremove(Object o)
Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
booleanremoveAll(Collection<?> c)
Removes from this Vector all of its elements that are contained in the specified Collection.
voidremoveAllElements()
Removes all components from this vector and sets its size to zero.
booleanremoveElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this vector.
voidremoveElementAt(int index)
Deletes the component at the specified index.
protected voidremoveRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
booleanretainAll(Collection<?> c)
Retains only the elements in this Vector that are contained in the specified Collection.
Eset(int index, E element)
Replaces the element at the specified position in this Vector with the specified element.
voidsetElementAt(E obj, int index)
Sets the component at the specified index of this vector to be the specified object.
voidsetSize(int newSize)
Sets the size of this vector.
intsize()
Returns the number of components in this vector.
List<E>subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
Object[]toArray()
Returns an array containing all of the elements in this Vector in the correct order.
<T> T[]toArray(T[] a)
Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
StringtoString()
Returns a string representation of this Vector, containing the String representation of each element.
voidtrimToSize()
Trims the capacity of this vector to be the vector's current size.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值