What is the difference between removeAllElements() and clear() method of DefaultListModel in java swing?
The java docs for DefaultListModel says :-
public void clear()
Removes all of the
elements from this list. The list will
be empty after this call returns
(unless it throws an exception).
and
public void removeAllElements()
Removes all components from this list
and sets its size to zero.
So both basically removes all elements from list so what is the difference? How to decide when to use which?
They are both same.
DefaultListModeluses aVectorunder the hood.The clear() method was added later when Vector was re-written to fit into the Collection API’s.
With version 1.3 the
Collections APImade its’ entrance so theVectorwas re-written to fit into theListinterface.In order for it to be backwards compatible, they simply forwarded the calls to the old existing methods where available & possible.
EDIT
From Java Source: