Java集合框架 Collection接口

本文深入探讨了Java集合框架中的ArrayList、LinkedList、Vector、Stack、HashSet和TreeSet等核心类的实现原理,包括它们的数据结构、扩容机制、增删查改操作细节。

1、ArrayList

ArrayList是List接口最常见的实现,其实现是基于数组的方式实现,无容量的限制,ArrayList是非线程安全的。

如下代码为ArrayList的构造函数,当没有确定的容量大小时,默认长度为10:

private transient Object[] elementData;    
public ArrayList(int initialCapacity) {
	super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
	this.elementData = new Object[initialCapacity];
    }
public ArrayList() {
	this(10);
    } 

 插入对象方法add(E e)把对象插入到数据尾部和add(int index, E element)把对象插入到指定位置index上:

    public boolean add(E e) {
	ensureCapacity(size + 1);  // Increments modCount!!
	elementData[size++] = e;
	return true;
    }

    public void add(int index, E element) {
	if (index > size || index < 0)
	    throw new IndexOutOfBoundsException(
		"Index: "+index+", Size: "+size);

	ensureCapacity(size+1);  // Increments modCount!!
	System.arraycopy(elementData, index, elementData, index + 1,
			 size - index);
	elementData[index] = element;
	size++;
    }

 插入对象都要做的就是先给对象扩容ensureCapacity(size+1):

    public void ensureCapacity(int minCapacity) {
	modCount++;
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object oldData[] = elementData;
	    int newCapacity = (oldCapacity * 3)/2 + 1;
    	    if (newCapacity < minCapacity)
		newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
	}
    }

 扩容后的空间有原来空间1.5倍+1。

 删除对象remove(int index)和remove(Object o):

    public boolean remove(Object o) {
	if (o == null) {
            for (int index = 0; index < size; index++)
		if (elementData[index] == null) {
		    fastRemove(index);
		    return true;
		}
	} else {
	    for (int index = 0; index < size; index++)
		if (o.equals(elementData[index])) {
		    fastRemove(index);
		    return true;
		}
        }
	return false;
    }

    public E remove(int index) {
	RangeCheck(index);

	modCount++;
	E oldValue = (E) elementData[index];

	int numMoved = size - index - 1;
	if (numMoved > 0)
	    System.arraycopy(elementData, index+1, elementData, index,
			     numMoved);
	elementData[--size] = null; // Let gc do its work

	return oldValue;
    }

    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // Let gc do its work
    }

 remove(Object o)采用的是遍历数组中的元素方式,指到等于传入的对象来删除,null与非null对象判断是否相等的方式是不一样的,然后都调用 fastRemove(int index)方法来删除;remove(int index)的删除跟remove(Object o)中的fastRemove(int index)删除方法一样,只是多了一个RangeCheck(index)数组范围检测和提供返回值。

 ArrayList在插入元素的时候可能要先扩容,在删除元素的时候并不会减小数组的容量,如果希望相应的减小数组容量,可以调用trimToSize()方法:

    public void trimToSize() {
	modCount++;
	int oldCapacity = elementData.length;
	if (size < oldCapacity) {
            elementData = Arrays.copyOf(elementData, size);
	}
    }

 

2、LinkedList

LinkedList也是 List接口常见的一种实现,也是非线程安全的,它是基于双向链表机制,所谓双向链表就是集合中的每个元素都知道其它前一个元素及后一个元素的位置。在 LinkedList中以一个内部的Entry类来代表集合中的元素,元素中的值赋给element属性,Entry中的next属性指向元素中的后一个 元素,previous指向无线的前一个属性。

    private transient Entry<E> header = new Entry<E>(null, null, null);    
    public LinkedList() {
        header.next = header.previous = header;
    }
    public LinkedList(Collection<? extends E> c) {
	this();
	addAll(c);
    }

 在创建LinkedList对象时先创建一个element属性、next属性、previous属性都为null的Entry对象并赋值给全局对象header。

LinkedList 的添加元素和删除元素也就是双向链表的操作,不像ArrayList那样考虑扩容、遍历和数据的复制问题,但它在插入元素时要创建一个新的Entry对 象。在删除元素时也要遍历链表,但是只需要只接删除链表上的元素即可而不像ArrayList那样实现数据复制。在查找元素时也必须要遍历链表。

 

 3、Vector

Vector是从JDK1.2开始实现的,Vector和ArrayList一样,也是基于Object方式来实现的,但它是线程安全的。它的线程安全是基于synchronized关键字来实现的,也就是在所有操作方法前面加上synchronized关键字:

    public synchronized int size() {
	return elementCount;
    }
    public synchronized void addElement(E obj) {
	modCount++;
	ensureCapacityHelper(elementCount + 1);
	elementData[elementCount++] = obj;
    }
    ......

 和ArrayList不同的是Vector的扩容方式,通过传入capacityIncrement来控制容量 的扩充,如果capacityIncrement大于0,则将Object数组的大小扩大为现有的size加上capacityIncrement,如果 capacityIncrement等于或小于0,则将数组的大小扩大为现有size的两倍,这种扩容方式比ArrayList更为可控。

    public Vector(int initialCapacity) {
	this(initialCapacity, 0);
    }
    public Vector(int initialCapacity, int capacityIncrement) {
	super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
	this.elementData = new Object[initialCapacity];
	this.capacityIncrement = capacityIncrement;
    }
    private void ensureCapacityHelper(int minCapacity) {
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object[] oldData = elementData;
	    int newCapacity = (capacityIncrement > 0) ?
		(oldCapacity + capacityIncrement) : (oldCapacity * 2);
    	    if (newCapacity < minCapacity) {
		newCapacity = minCapacity;
	    }
            elementData = Arrays.copyOf(elementData, newCapacity);
	}
    }

 

4、Stack

Stack继承于Vector,在Vector的基于上实现了栈的后进先出(LIFO)算法,提供了push、pop、peek等方法,Stack也是线程安全的。

 

5、HashSet

HashSet是Set接口的常见实现,Set和List最明显的区别是Set不充许有重复的元素,而List则充许。Set的内部实现是基于HashMap来实现的,Set中的元素就是HashMap中的Key,其容易是不限制的,HashSet是非线程安全的。

    public HashSet() {
	map = new HashMap<E,Object>();
    }
    public boolean add(E e) {
	return map.put(e, PRESENT)==null;
    }

 

 6、TreeSet

TreeSet也是Set接口的一种实现,也是非线程安全的,和HashSet主要区别是TreeSet对排序的支持,TreeSet基于TreeMap实现:

    private transient NavigableMap<E,Object> m;
    TreeSet(NavigableMap<E,Object> m) {
        this.m = m;
    }
    public TreeSet() {
	this(new TreeMap<E,Object>());
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值