Vector Example Special Operations

Vector Example Special Operations

Four programs are given on Vector to know thoroughly about Vector, methods and usage. Vector is a general purpose data structure used very often by the programmer even though the collections framework comes with many classes.

  1. Vector – Methods: Uses all the methods of Vector class like capacity, size, firstElement etc.
  2. Generics Vector: Create generics vector storing only integers and uses all Collections static methods like swap, shuttle, fill, rotate etc.
  3. Vector – Retrieval: Illustrates some more methods like get, remove, removeAll and also how to retrieve the elements – 6 styles
  4. Vector Example Special Operations: Give manipulations on vector elements like removing duplicate values, converting vector elements into an array etc.

Note: At the end of this tutorial, see the technique of avoiding duplicate elements in Vector. There is no direct method to eliminate in Vector.

It advised to go through the first program Vector Methods where vector methods, constructors and history are discussed.

    Vector Example Special Operations

    In this program, special operations like converting vector elements into array elements, removing duplicate elements and printing the vector elements in ascending order etc. are discussed. All are done in simple code using predefined methods of collections classes (avoiding C/C++ lengthy code of iterations)

    Output screen of Vector Example Special Operations

    vect1 elements are converted into an array with toArray() method, inherited from Collection interface. With enhanced for loop (Java 5) all the elements are iterated and printed.

    A generics vector object vect2 is created and is converted into string array greetings. It is another style of converting vector into an array, but laborious.

    Next task is eliminating duplicate elements in a vector. Generally, a programmer does a lot of loop iterations to do the task. But in Java, many methods are available in collections framework and all collections classes are interoperable. We know Set interface subclasses like TreeSet, HashSet do not accept duplicate elements. This property is taken to eliminate the duplicates. Vector is converted into HashSet. HashSet automatically removes duplicates. Again HashSet is converted back to Vector using addAll() method inherited from Collection interface.

    Vector vect3 is created to store string objects. subList() method (inherited from List interface) is used to convert vector elements into List, myList. Again myList is converted into HashSet, mySet. mySet elements are added to another vector vect4. Now vect4 contains unique elements.

    For extra functionality, myList is converted into TreeSet. TreeSet nature is to print the elements in ascending order by default.

    Know the tree structure of Know Java Collections Interfaces Hierarchy

2 thoughts on “Vector Example Special Operations

Leave a Reply

Your email address will not be published. Required fields are marked *