Vector Play With

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

Note: See the technique of avoiding duplicate elements in Vector in the code.

  1. Vector – Methods: Uses all the methods of Vector class like capacity, size, firstElement etc.
  2. Vector – Generics: 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 – Play With: Give manipulations on vector elements like removing duplicate values, converting vector elements into an array etc.

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

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.

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.

Java Stack ExamplesJava Stack, Java Generics Stack

General Exceptions raised by collections classes

IllegalStateException ConcurrentModificationException
UnsupportedOperationException NullPointerException

Know Java Collections Interfaces Hierarchy
Know Java Interface Collection
Know Enumeration vs Iterator
Know Iterator vs ListIterator
Know Vector vs ArrayList

2 Responses

  1. manish February 19, 2015 / 12:44 pm

    i am a big fan of you and your website. can you suggest name of any institute or website for Android . i wan to earn Android .

    with thanks

Leave a Reply

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