Sunday, 14 December 2014

Java : Collection Framework : Vector (trimTosize)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=aLvOjZ2J9C0&list=UUhwKlOVR041tngjerWxVccw

VectorExample.java
import java.util.Vector;

/*
 *  Example of trimToSize() method. 
 */
public class VectorExample
{
    public static void main(String[] args)
    {
        Vector<Integer> vector = new Vector<Integer>();

        System.out.println("capacity : " + vector.capacity() + "\n");

        for (int i = 0; i < 12; i++)
        {
            vector.add(i);
        }

        System.out.println("vector : " + vector + "\n");

        System.out.println("size : " + vector.size());
        System.out.println("capacity : " + vector.capacity());

        /*
         * Trims the capacity of this vector to be the vector's current size. If
         * the capacity of this vector is larger than its current size, then the
         * capacity is changed to equal the size by replacing its internal data
         * array, kept in the field elementData, with a smaller one.
         * 
         * An application can use this operation to minimize the storage of a
         * vector.
         */
        vector.trimToSize();

        System.out
                .println("After trimToSize : capacity : " + vector.capacity());

    }
}

Output
capacity : 10

vector : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

size : 12
capacity : 20
After trimToSize : capacity : 12

To Download VectorDemoTrimToSize Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/VectorDemoTrimToSize.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment