1

While changing some code based on SonarQube suggestions I get to know below lines:

  1. Automatic Increase in Capacity A Vector defaults to doubling size of its array . While when you insert an element into the ArrayList ,it increases its Array size by 50%.

Now I am wondering if I need to replace the Vector with ArrayList there is a chance of failure of normal execution of the code.

Remember existing Vector is not doing any Thead-safe work.

Question:

  1. Is ArrayList capable enough to resize just like vector?

  2. Is it safe to replace the Vector with ArrayList in any condition except Synchronization??

  3. Is there any exact replacement of Vector (Not expecting the Thread-safety)

Please feel free to update the question or ask anything.

MonsterJava
  • 423
  • 7
  • 23
  • What failure do you expect? – assylias Mar 18 '16 at 10:25
  • 3
    [Vector is broken](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated), you **should** use `ArrayList`, and if you need synchronization use `synchronizedList`. – Idos Mar 18 '16 at 10:26
  • In existing code a List (No maximum limit) of URL's has been added in Vector. I afraid if I use ArrayList that has 50% increment may break the feature. – MonsterJava Mar 18 '16 at 10:28
  • I dont think it is broken. Its legacy code, we cant 100% sure of its EOL I guess. @Idos – MonsterJava Mar 18 '16 at 10:30
  • 1
    @MonsterJava ArrayList will resize automatically to hold as many URLs as you want (assuming you have enough memory - but it won't use more memory than Vector on average). – assylias Mar 18 '16 at 10:30

2 Answers2

4

The differences between Vector and ArrayList are some like this:

  1. Vector is synchronized while ArrayList is not synchronized. So, Vector is thread safe.
  2. Vector is slow as it is thread safe . In comparison ArrayList is fast as it is non-synchronized.
  3. A Vector grows as doubling size of its array in default. While when you insert an element into the ArrayList, it increases its Array size by 50% .

    • ArrayList:

      /**
       * Increases the capacity to ensure that it can hold at least the
       * number of elements specified by the minimum capacity argument.
       *
       * @param minCapacity the desired minimum capacity
       */
      private void grow(int minCapacity) {
         // overflow-conscious code
         int oldCapacity = elementData.length;
         int newCapacity = oldCapacity + (oldCapacity >> 1); // 50%
         if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
         if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
         // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
      }
      
    • Vector:

      private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                       capacityIncrement : oldCapacity); // default 100%
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
      }
      
  4. ArrayList does not define the increment size . Vector defines the increment size .

        /**
         * The amount by which the capacity of the vector is automatically
         * incremented when its size becomes greater than its capacity.  If
         * the capacity increment is less than or equal to zero, the capacity
         * of the vector is doubled each time it needs to grow.
         *
         * @serial
         */
         protected int capacityIncrement;
    

Based above:

  1. ArrayList capable can not resize just like Vector.
  2. ArrayList is not thread safe. It can not replace Vector by ArrayList in multiple threads directly.
  3. It can replace Vector by ArrayList in single thread mostly. Because The declaration of Vector and ArrayList:

    public class Vector<E>
        extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable  
    
    
    public class ArrayList<E> 
        extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable   
    
Yanhui Zhou
  • 872
  • 6
  • 20
0

I don’t see a problem. The exact performance metrics of Vector and ArrayList are not the same, but for most practical purposes this is not important. The ArrayList will extend whenever needed, and more often than Vector (if you don’t tell it the needed capacity beforehand). Go ahead.

For your questions: 1. Yes 2. Yes 3. No

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161