开发者

Why Java DefaultTableModel use Vector?

开发者 https://www.devze.com 2023-03-01 20:45 出处:网络
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 stu

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.2
  • ArrayList was introduced in Java 1.2
  • Vector 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消