Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the Vector source code

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to parse the Vector source code, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Among the collection classes of Java, in addition to ArrayList, there are some other collection classes that are also implemented in arrays at the bottom, such as the Vector collection class. So what is the difference between them in use or in the underlying implementation? Next we will analyze the relevant knowledge of the Vector collection in detail.

Let's directly analyze the underlying source code of Vector to compare the underlying differences between the Vector collection and the ArrayList collection. Just like the ArrayList collection, let's first look at the initialization of the Vector collection, that is, the constructor.

Initialization

Although there are three different constructors above, we can see from the method call relationship that if we use a no-parameter constructor to create a Vector object, then the above three methods will be executed in turn. And we know that the core of the above method, that is, the code that initializes the array, is the third constructor. Because we have analyzed the source code in ArrayList, we can easily draw the following conclusions:

The Vector collection, like ArrayList, sets the size of the underlying array to 10 when the collection performs default initialization for the first time

The difference between the Vector collection and the ArrayList is that the ArrayList collection does not perform default initialization for the array when we create an object using the no-parameter constructor. The first default initialization occurs when the ArrayList collection calls the add () method for the first time. In the Vector collection, when we create an object using the no-parameter construction method, the default initialization is performed immediately.

Let's analyze the implementation logic of the add () method of the Vector collection.

Dynamic allocation

The above code seems familiar to us, isn't it? yes, the above code logic is actually almost the same as the ArrayList source logic. The only difference is that the dynamic allocation of Vector collections and ArrayList collections is different. In ArrayList's article, we already know that when the underlying array has reached its maximum capacity, an array that is 1.5 times larger than the original array is automatically created. So in the above code, we find that the underlying extension of the Vector collection is to create an array twice as large as the original array to store elements. This is one of the differences between Vector collections and ArrayList collections. Since it is one of the differences, then there must be other differences. We found that the synchronized synchronization keyword was added to the add () method, which means that the Vector collection is a thread-safe collection class, which is also one of the differences from the ArrayList collection, because the ArrayList collection is not a thread-safe collection class.

Matters needing attention

Because the bottom layer of the Vector collection is also implemented as an array, its disadvantages are basically the same as the ArrayList collection, so I won't talk too much about it here. It is important to note, however, that because the Vector collection is a thread-safe collection class, we can use it directly without adding additional synchronized code to ensure thread safety when developing multithreading. However, if our development is not running in a multithreaded environment, if we continue to use the Vector collection, it will cause a decline in the performance of our program, this is because the bottom layer of the Vector collection uses the ynchronized synchronization keyword, so every time we execute the add () method, we frequently perform the acquisition lock and release lock operations, which will affect the performance of the program invisibly. Therefore, in our daily development, if we do not use List collections in a multithreaded environment, then we recommend using ArrayList or LinkedList collections, because they are not thread-safe collection classes, so they do not frequently perform lock acquisition and release operations, so the program runs more efficiently than Vector collections.

On how to parse the Vector source code to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report