And if you use it often?
PS Why use Vector if there is a stack?

Closed due to the fact that the issue is too general for the participants of Kromster , aleksandr barakin , Denis , HamSter , pavel Nov 15, '16 at 8:18 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Of course used. Where it is needed. And where not needed - other suitable data structures (read what they are and why they are suitable) - Kromster
  • No need to use them, there are analogues better in the standard library. By the way, in version 9 of vector and stack it is planned to start openjdk.java.net/jeps/277 - Artem Konovalov

1 answer 1

The methods of the Vector class are synchronized, but ArrayList is not. Accordingly, ArrayList methods are faster than Thread-safe Vector methods.

There are also some differences in the implementation of the collection itself. For example, scaling up an ArrayList made half the size of the current collection, and 'Vector' is doubled.

So sometimes Vector can be used if you need a collection with individually synchronized methods. With Stack all about as well.

UPD: The question has changed. I answer:

Stack extends the Vector class. The Stack implements the methods empty, peek, push, pop, search. That is, typical stack operations.

Of course, he keeps in himself all the methods of Vector , but to say that Vector not needed, since there is a Stack I would not. Stack is made for convenience. You can use it instead of Vector Stack , but it's better to call things by their proper names. You will not become a variable that stores a reference to an object of class Boy to call myLittleGirl ?

  • I meant if there is a stack, is a vector needed at all? - Anton Sorokin
  • For fast wrappers in synchronized access, there is a family of methods Collections.synchronized* , which levels the value of this difference - etki
  • @ antonsorokin changed the answer. - GreyGoblin