wood burning stoves 2.0*
The moose likes Java in General and the fly likes Why Vector is made as a Legacy Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Vector is made as a Legacy Class" Watch "Why Vector is made as a Legacy Class" New topic
Author

Why Vector is made as a Legacy Class

Praveen Srinivasan
Greenhorn

Joined: Aug 28, 2010
Posts: 17
Why vector is made as a legacy class , what are the complexities in using Vector nowadays, what are the reasons to make it obsolete..anyone explain it...


Preparing for SCJP6.0
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Vector is the same as ArrayList, except it is synchronized. In most cases you don't need this synchronization. In the few cases where you do, you can either use Vector or use an ArrayList wrapped using Collections.synchronizedList.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Praveen Srinivasan
Greenhorn

Joined: Aug 28, 2010
Posts: 17
Is it the only reason...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

For General:
vector is having intial capacity of 10.but in some cases we donot need to add that much element in a vector.
So this is the disadvantage of using vector.

Note:-Thats a differnt thing that you having a construtors by which you can choose your own capacity and also capacity increment


SCJP6.0,My blog Ranchers from Delhi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Shanky Sohar wrote:For General:
vector is having intial capacity of 10.but in some cases we donot need to add that much element in a vector.
So this is the disadvantage of using vector.

Note:-Thats a differnt thing that you having a construtors by which you can choose your own capacity and also capacity increment

With your note you immediately invalidate your earlier "reason". I can create both an ArrayList and a Vector with a capacity of just 1 if needed.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

using vector in a class doesnot make the class as Thread safe until and unless whole the methods of the class is not synchronized.

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Rob Prime wrote:
Shanky Sohar wrote:For General:
vector is having intial capacity of 10.but in some cases we donot need to add that much element in a vector.
So this is the disadvantage of using vector.

Note:-Thats a differnt thing that you having a construtors by which you can choose your own capacity and also capacity increment

With your note you immediately invalidate your earlier "reason". I can create both an ArrayList and a Vector with a capacity of just 1 if needed.


Yes ,i know that..that is the reason why i put a note
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Shanky Sohar wrote:using vector in a class doesnot make the class as Thread safe until and unless whole the methods of the class is not synchronized.
That totally depends on the situation.

Vector and Collections.synchronized*() are not deprecated but it's recommended to use the classes in the java.util.concurrent.* package;


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Wouter Oet wrote:
Shanky Sohar wrote:using vector in a class doesnot make the class as Thread safe until and unless whole the methods of the class is not synchronized.
That totally depends on the situation.

Vector and Collections.synchronized*() are not deprecated but it's recommended to use the classes in the java.util.concurrent.* package;


Agreed.
But when we have to apply a high level synchronization on the class .and make the class as highly ThreadSafe
then we have to make each and every method as synchronized.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 516
    
    4

Making each and every method synchronized is just bad practice. You only synchronize the methods where your program's state might be corrupted by concurrent access; or where concurrent access might render imprecise or faulty data to the user at a given time.

In fact, I think Oracle might really improve this situation in Java. Their databases are known for their brilliance in concurrency.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Dieter Quickfend wrote:Making each and every method synchronized is just bad practice. You only synchronize the methods where your program's state might be corrupted by concurrent access; or where concurrent access might render imprecise or faulty data to the user at a given time.

In fact, I think Oracle might really improve this situation in Java. Their databases are known for their brilliance in concurrency.


I agree with you about blindly synchronizing every method. But we don't need to wait for Oracle to act; it was about 12 years ago that Sun engineers dealt with that problem by releasing the Java Collections framework. Java 2, I believe, was when that was released. And then in Java 5 (what was that, 5 years ago now?) the java.util.concurrent package was added with even better concurrency options. So there really isn't much use for Vector any more, except as a topic for discussion by generations of beginners.
Yogesh Tyagi
Ranch Hand

Joined: Apr 30, 2004
Posts: 39

Praveen Srinivasan wrote:Why vector is made as a legacy class , what are the complexities in using Vector nowadays, what are the reasons to make it obsolete..anyone explain it...


Praveen, Try to understand the meaning of the word "Legacy", then you will understand it yourself in case of Vector, Hashtable or in some other contexts.


Yogesh Tyagi
SCJP1.5, SCWCD5.0, SCBCD5.0
 
jQuery in Action, 2nd edition
 
subject: Why Vector is made as a Legacy Class
 
Similar Threads
How to Iterate over a hashMap
Legacy Methods
Is Vector a point of the Exam 1.4 ?
Legacy classes
Vector reference?