List collection --Vector subclass

Vector subclass of
  Vector is an original old classes, this class is in JDK1.0 when he offered, then the time to JDK1.2, since part of the developers have become accustomed to using the Vector, and many of the class system is based on consideration of the extensive use of Vector implemented, so cluster framework will save it down, and allowed to achieve a multi-interface List.
- observation of the Vector definition structure

- can be found in the inheritance structure is the same as the ArratList

 1 public class VectorDemo {
 2     public static void main(String[] args) {
 3         List<String> all = new Vector<>();
 4         all.add("hello");
 5         all.add("hello");
 6         all.add("world");
 7         all.add("test");
 8         all.forEach(System.out::println);
 9     }
10 }

--operation result

hello
hello
world
test

Process finished with exit code 0

- further observation to achieve the Vector class:

public Vector() {
this(10);
}
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;
}

If --Vector class constructor is no argument, it will open up a length of the array 10, and then the rest of the operation to achieve the same ArrayList. By viewing the source, can be found in the methods employed are Vectory synchronized processing is synchronized, but ArrayList and not being synced, so when the Vector method in a multithreaded visit was part of thread-safe, but the performance is not as high ArrayList.

Guess you like

Origin www.cnblogs.com/skykuqi/p/11446534.html