JavaUser
JavaUser

Reputation: 26324

Difference between Java's Vector.add() and Vector.addElement()?

Please explain the difference between the Vector.add() method and the Vector.addElement() method, along with a sample code snippet

Upvotes: 19

Views: 28943

Answers (5)

Sai Kiran
Sai Kiran

Reputation: 301

main difference -> add() will always return true, while addElement() has no return value.

in dept: addElement(object) method is identical in functionality to the add(Object) method (which is part of the List interface).

add(Object ) is due the fact that Vector implements List Interface and it is appeared since Java 1.2 when Vector was moved to Collections: The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection interfaces.

addElement is "original" Vector's method.

found this answer here.. What is difference between add() and addElement() in Vector?

Upvotes: 0

cletus
cletus

Reputation: 625037

add() comes from the List interface, which is part of the Java Collections Framework added in Java 1.2. Vector predates that and was retrofitted with it. The specific differences are:

  1. addElement() is synchronized. add() isn't. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection in Collections.synchronizedList(); and

  2. add() returns a boolean for success. addElement() has a void return type.

The synchronized difference technically isn't part of the API. It's an implementation detail.

Favour the use of the List methods. Like I said, if you want a synchronized List do:

List<String> list = Collections.synchronizedList(new ArrayList<String>());
list.add("hello");

Upvotes: 29

OscarRyz
OscarRyz

Reputation: 199215

addElement

This method is identical in functionality to the add(Object) method (which is part of the List interface).

So there is no difference between:

Vector v = new Vector();
v.addElement( new Object() );

and

Vector v = new Vector();
v.add( new Object() );

This class ( vector ) exists since Java1.0 and now is pretty much replaced by ArrayList which has the benefit of being slightly faster.

Upvotes: 1

Jubal
Jubal

Reputation: 8717

The method signature is different, add returns true, while addElement is void.

from http://www.docjar.com/html/api/java/util/Vector.java.html

  153       public synchronized boolean add(E object) {
  154           if (elementCount == elementData.length) {
  155               growByOne();
  156           }
  157           elementData[elementCount++] = object;
  158           modCount++;
  159           return true;
  160       }

and

223       public synchronized void addElement(E object) {
  224           if (elementCount == elementData.length) {
  225               growByOne();
  226           }
  227           elementData[elementCount++] = object;
  228           modCount++;
  229       }

Upvotes: 6

Michael Mrozek
Michael Mrozek

Reputation: 175355

The javadoc mentions that:

public void addElement(E obj)

This method is identical in functionality to the add(E) method (which is part of the List interface).

The reason they both exist is (from the same javadoc):

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework.

List has an add method, so an implementation was added to Vector, but to maintain backwards-compatibility, addElement wasn't removed

Upvotes: 5

Related Questions