Please explain the difference between the Vector.add() method and the Vector.addElement() method, along with a sample code snippet
Please explain the difference between the Vector.add() method and the Vector.addElement() method, along with
Share
add()comes from theListinterface, which is part of the Java Collections Framework added in Java 1.2.Vectorpredates that and was retrofitted with it. The specific differences are:addElement()issynchronized.add()isn’t. In the Java Collections Framework, if you want these methods to be synchronized wrap the collection inCollections.synchronizedList(); andadd()returns a boolean for success.addElement()has avoidreturn type.The
synchronizeddifference technically isn’t part of the API. It’s an implementation detail.Favour the use of the
Listmethods. Like I said, if you want asynchronizedListdo: