Source code analysis notes Vector

Outline

AbstractList abstract class inheritance, implements the interface List, RandomAccess, Cloneable serial interfaces and
the default capacity size is 10, 0 increments expansion, the expansion is twice the original volume
increment is set as larger than 0, the expansion of the (original capacity + incremental)
support random access, add and delete elements of the slower
thread-safe dynamic arrays, coupled with the synchronized method synchronized lock, so the lower performance
Source

Field Information

the Vector class public <E>
the extends AbstractList <E>
the implements List <E>, the RandomAccess, the Cloneable, the java.io.Serializable
{
/ **
* save data array
* /
protected Object [] elementData of;

/ **
* number of elements
* /
protected int to the elementCount;

/ **
* Incremental capacity, you can specify the expansion, the capacity to expand the number
* /
protected int capacityIncrement;

/ * ** serialization ID /
Private Long serialVersionUID = Final static -2767605614048989439L;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
the constructor

/ *
* Main construction method, this method is invoked is inside
* initial capacity initialCapacity
* of capacityIncrement increments, each time increasing the expansion capacity size
* /
public the Vector (initialCapacity int, int of capacityIncrement) {
Super ();
// Analyzing legitimacy of the initial capacity
IF (initialCapacity <0)
the throw new new an IllegalArgumentException ( "Illegal capacity:" +
initialCapacity);
// Create Object array initialCapacity size
this.elementData = new new Object [initialCapacity];
// increment size specifies
this. = of capacityIncrement of capacityIncrement;
}

/ **
* specified initial capacity, but does not specify an increment
* increments i.e. 0
* /
public the Vector (int initialCapacity) {
// call the constructor of the two parameters
the this (initialCapacity, 0);
}

/ **
* constructor with no arguments, the default initial capacity of 10 increments 0
* /
public the Vector () {
the this (10);
}
/ **
* pass another set, the length of the incoming set of increments
* /
public the Vector (Collection C <the extends E?>) {
elementData of c.toArray = ();
to the elementCount = elementData.length;
// c.toArray Might (incorrectly) Not return Object [] (See 6260652)
IF (elementData of. ! getClass () = Object [] class).
elementData of Arrays.copyOf = (elementData of, to the elementCount, Object [] class);.
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
added element

/ **
* additive element
* /
public Boolean the synchronized the Add (E E) {
// modify records the number of array
ModCount ++;
// capacity check, it is determined whether expansion
ensureCapacityHelper (to the elementCount +. 1);
// add elements
elementData [elementCount ++] = E;
return to true;
}

/ **
* Add the specified location element
* /
public void the Add (int index, E Element) {
insertElementAt (Element, index);
}

the synchronized void insertElementAt public (E obj, int index) {
ModCount ++;
// check the validity of the subscript, index not greater than the number of elements
IF (index> to the elementCount) {
the throw new new An ArrayIndexOutOfBoundsException (index
+ ">" to the elementCount +);
}
/ / capacity check
ensureCapacityHelper (to the elementCount +. 1);
// copy array
// the index, length of the array elementCount-index, a backward movement
System.arraycopy (elementData, index, elementData, index + 1, elementCount - index );
elementData of [index] = obj;
to the elementCount ++;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
expansion operation

/ **
* capacity check, it is determined whether capacity is needed
* /
Private void ensureCapacityHelper (int be minCapacity) {
// overflow-Conscious code
IF (be minCapacity - elementData.length> 0)
Grow (be minCapacity);
}

/ **
* Maximum capacity threshold, the maximum capacity of Integer.MAX_VALUE
* /
Private static int MAX_ARRAY_SIZE = Final Integer.MAX_VALUE -. 8;

/ **
* The actual expansion methods
* /
Private void Grow (int be minCapacity) {
// the current capacity
int = oldCapacity elementData.length;
// calculate new capacity
// If no incoming increment size, the current capacity expansion 2 times (2 * oldCapacity)
// if the increment size is provided, if the increment is less than 0, the expansion of 2 times; if more than 0, the expansion of (oldCapacity + increment size)
int = oldCapacity newCapacity + ((of capacityIncrement> ? 0)
of capacityIncrement: oldCapacity);
// new capacity is determined whether the size meets a minimum required capacity, if not satisfied, direct the expansion of the capacity size required
iF (newCapacity - be minCapacity <0)
newCapacity be minCapacity =;
// Analyzing reaches the maximum capacity threshold
iF (newCapacity - MAX_ARRAY_SIZE> 0)
newCapacity = hugeCapacity (be minCapacity);
// array expansion, the greater the cost of the method, the array size expansion is newCapacity
elementData of Arrays.copyOf = (elementData of, newCapacity);
}

static int hugeCapacity Private (int be minCapacity) {
// determines whether the required minimum capacity integer overflow
IF (be minCapacity <0) // overflow
the throw an OutOfMemoryError new new ();
// size of the maximum capacity expansion Integer.MAX_VALUE
return (be minCapacity> ? MAX_ARRAY_SIZE)
Integer.MAX_VALUE:
MAX_ARRAY_SIZE;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
Summary

The default size is 2 times the original expansion volume; provided delta is greater than 0, the expansion of the size compared with the original capacity plus the increment,
the maximum capacity of Integer.MAX_VALUE, threshold is Integer.MAX_VALUE-8
add sequence elements: the incoming desired capacity size - whether> Analyzing capacity is needed -> performs expansion operation (calculating the size of the new capacity, call Arrays.copyOf (object [], newCapacity) ) -> the additive element
due Arrays.copyOf () the cost of large, so creating Vector You should try to set the size of the collection
to remove elements

/ **
* removing elements according to the specified index, return the value of the removed element
* /
public the synchronized E Remove (int index) {
ModCount ++;
IF (index> = to the elementCount)
the throw new new An ArrayIndexOutOfBoundsException (index);
// save the index obtaining subscripts element value
E = elementData of oldValue (index);
// calculate the length of the moving copy arrays
int = numMoved to the elementCount - index -. 1;
// length greater than 0, then the index index of the element behind the copy and move forward one position
if (numMoved > 0)
System.arraycopy (elementData of,. 1 + index, elementData of, index,
numMoved);
// new indexing position vacated filling is null, so that the memory can be recovered gc
elementData [- elementCount] = null; // Let gc do its work

oldValue return;
}
/ **
* delete a specific element value, because the presence of the collection element uncertain, it returns a boolean value, whether to delete returns success
* /
public boolean Remove (Object O) {
return removeElement (O);
}

Boolean removeElement the synchronized public (Object obj) {
ModCount ++;
// Get the value of the first occurrence of the specified element subscript, not found -1 is returned
int I = the indexOf (obj);
IF (I> = 0) {
// The remove elements specified index
removeElementAt (i);
return to true;
}
// specified element is not found, returns false, the delete fails
return false;
}

/ **
* The subscript remove elements, no return value
* /
public void the synchronized removeElementAt (int index) {
ModCount ++;
// index determines whether valid
IF (index> = to the elementCount) {
the throw new new An ArrayIndexOutOfBoundsException (index + "> = "+
to the elementCount);
}
the else IF (index <0) {
the throw new new An ArrayIndexOutOfBoundsException (index);
}
// same Remove (index)
int = J to the elementCount - index -. 1;
IF (J> 0) {
System.arraycopy (elementData of ,. 1 + index, elementData of, index, J);
}
elementCount--;
elementData of [to the elementCount] = null; / * Work to the let GC do ITS * /
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54 is
55
56 is
57 is
58
59
60
61 is
Summary

remove (int index) and removeElementAt (int index) to return the difference values to remove elements remove method, but no return value removeElementAt
remove the elemental content may not successfully deleted, may not be the target element in the set
manually volume reduction

/ **
* Manually reduces the capacity of the internal array
* /
public void the synchronized trimToSize (http://www.amjmh.com) {
ModCount ++;
int = oldCapacity elementData.length;
// number of elements in the array determines the length of the array size
if ( to the elementCount <oldCapacity) {
elementData of Arrays.copyOf = (elementData of, to the elementCount);
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
manually expansion

/ **
* pass a capacity size, set greater than the current capacity, the expansion
* /
public void the ensureCapacity the synchronized (int minCapacity) {
IF (minCapacity> 0) {
ModCount ++;
// determines whether minCapacity array is greater than the capacity, whether expansion operation
ensureCapacityHelper (be minCapacity);
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
disposed set size

/ **
* Set the size of capacity
* array if greater than the current capacity, the expansion
* is less than the current capacity of the array, the operation will be partially filled is null, so that the part of the memory can be recovered gc
* /
public void the synchronized the setSize (int newSize) {
ModCount ++;
IF (newSize> to the elementCount) {
ensureCapacityHelper (newSize);
} the else {
for (int I = newSize; I <to the elementCount; I ++) {
elementData of [I] = null;
}
}
to the elementCount = newSize;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
comparison and ArrayList

Vector is thread-safe, in the method plus synchronize synchronization lock, so the access speed is lower than the ArrayList slow performance; the best use ArrayList, because synchronous operation can have to control our own
ArrayList expansion of 1.5 times (oldCapacity >> 1), Vector is twice the default
summary

When you use the best size specified capacity
does not own volume reduction when deleting elements
---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/11257591.html