Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector and ArrayDeque classes

What is the diffirence between Vector and ArrayDeque classes? I read about the ArrayDeque class yesterday, while I have used Vector class before.

like image 487
Kamotho Avatar asked Sep 09 '26 12:09

Kamotho


1 Answers

The basics are:

Vector implements java.util.List, which defines containers that allow index-based access to elements. It also implements interface RandomAccess, which indicates to the user that the underlying representation allows fast (typically O(1)) access to elements.

ArrayDeque implements java.util.Deque, which defines a container that supports fast element adding and removal from the beginning and end of the container.

Major differences:

  • Vector supports adding elements into the middle of the container, using the overloaded versions of List.add(int index, E element) or List.addAll(int index, Collection<? extends E> c).

  • Vector supports removing elements from the middle of the container, using the remove method.

  • Vector's set and setElementAt methods allow you to do an in-place element swap (replace one object in the Vector with another one, an O(1) operation).

  • adding to the end of a Vector is amortized constant time. Adding to the beginning or middle of the vector is a linear time operation (O(n)).

  • ArrayDeque has amortized constant time (O(1)) adding/deletion of elements at both the front and back of the container.

  • ArrayDeque does not allow you to specifically remove an element at a certain position in the container. The various remove, removeFirst, and removeLast methods of the class allow you a slightly more limited element removal.

  • ArrayDeque comes with methods for using the class like a queue (peek, poll, add, addFirst) and like a stack (offer, push, pop, peekLast, addLast), or like both (hence why it is a Double-Ended Queue).

  • ArrayDeque has no support for adding elements into the middle of the deque.

  • Vector has special ListIterators which allows you to get an iterator that starts at a specific location in the container, and also support adding, removing, and setting elements. ArrayDeque's iterators do not support those extra methods.

  • Vector is a synchronized container, meaning that it already contains code to perform synchronization/locking for multithreaded environments. For ArrayDeque, you have to provide your own synchronization code if you're doing multithreaded access to the container. Note that ArrayList is an unsynchronized counterpart to Vector.

like image 145
逆さま Avatar answered Sep 11 '26 01:09

逆さま



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!