开发者

Is Collections.syncronizedList effectively a vector?

开发者 https://www.devze.com 2023-04-08 20:01 出处:网络
Does Collections.syncronizedList(new ArrayList()) effectively makes the arrayList a vector? If no then what extra does it do?

Does Collections.syncronizedList(new ArrayList()) effectively makes the arrayList a vector? If no then what extra does it do? If yes then why was it introduced in the first place?

开发者_JAVA技巧

Thanks.


It provides a synchronized view (a wrapper) of any list. The differences:

  • you can make a synchronized view of any list implementation. A synchronized LinkedList is not a Vector.
  • the original list is not synchronized - only the view is.


  1. It doesn't make a list something else, but wraps a List with something that looks like a list also, but contains no data except the wrapped list.
  2. See 1.


Collections.synchronizedList(...) wraps synchronized access to the underlying list, but still retains the underlying list's characteristics in other ways. So for example, if you wrap a LinkedList, the iterator() remove() will be an efficient operation. (The same operation on a Vector is O(n).)

So the wrapper is absolutely not the same as a Vector.

0

精彩评论

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

关注公众号