I know we have to use AWT thread for all table model update operations. Under the single AWT thread, any table model will be thread-safe. Why DefaultTableModel picks thread-safe Vector as its data stuc开发者_如何学运维ture, which slower than other data structures like ArrayList?
Swing first appeared before Java 1.2, so before ArrayList
was available. Unfortunately, the API for DefaultTableModel
exposes the fact that it uses Vector
, so changing it now would be backwardly incompatible.
This is exactly the kind of reason for thinking about encapsulation carefully - it lets you change the internals later on. (Admittedly getting serialization right would have been interesting, but that's a story for another day...)
Swing was available for, but not included in, Java 1.1. List
/ArrayList
was introduced in 1.2. Pity, because Swing could have done with a bit of extra time before locking down the API.
The reason has already been explained above (Swing existed before java.util Collections library).
The bottom line is: never use DefaultTableModel
but rather build your own (based on AbstractTableModel
).
I'm going to guess that the DefaultTableModel
class was actually developed before The Collections Framework (which includes the ArrayList
class) was introduced in Java -- therefore, the DefaultTableModel
class wasn't implemented using the classes introduced as part of The Collections Framework.
Here are a few facts:
DefaultTableModel
was introduced in Java 1.2ArrayList
was introduced in Java 1.2Vector
was introduced in JDK 1.0
Furthermore, the use of a Vector
as the underlying data structure by the DefaultTableModel
class is an implementation detail, as the TableModel
interface itself doesn't rely on the usage of a Vector
.
精彩评论