6

I'm writing some code to do some SSH jobs on a remote machine and one of the methods I'm (forced of) using returns a Vector type. I've learned about them several years ago but never had the chance of actually using them until now.

What I do know about them is

  1. They are not thread safe
  2. They are very old and are deprecated (Netbeans warns me it's an "Obsolete Collection")

Not being thread-safe seems not that of a big of a deal if you're being careful about it. but I don't think it's enough to dismiss them. Am I missing something?

EDIT:

It seems I've touched a serious issue here with Vectors being obsolete(deprecated may not be the right word), what I want to know is why are vectors being so avoided in the first place

3

1 Answer 1

8

In Java8 Vector's source, there is no depracated notice.

There is the following comment in the header :

Unlike the new collection implementations, {@code Vector} is synchronized. If a thread-safe implementation is not needed, it is recommended to use {@link ArrayList} in place of {@code Vector}.

Fuerthermore you can use Collections.synchronisedList() with an ArrayList as parameter to replace what a vector is able to do.

But otherwise, no Vector works well.

If you really don't like it do :

List l = new ArrayList(myFunctionThatReturnAVector()); 
1
  • Vector also implements Serializable whereas Collections.synchronizedList() returns java.util.List which does not implement Serializable. Similar situation for Hashtable vs synchronizedMap(). Commented Sep 13, 2024 at 22:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.