Difference between vector and arraylist in Java?

Source: Internet
Author: User

I always asked a question during the interview. What is the difference between vector and arraylist?

I have never paid too much attention to this problem before. Therefore, when querying materials, I can see that the explanations for this problem on the Internet are almost the same (they should all be copied. This time I found a foreigner's explanation, which is a simple image. It's very easy to repost it. Http://service.ap-southeast-3.maxcompute.aliyun-inc.com

Http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/

If you are interested, you can check it yourself.

Java. util. vector came along with the first version of Java Development Kit (JDK ). java. util. arraylist was introduced in Java version1.2, as part of Java collections framework. as per Java API, in Java 2 platform v1.2, vector has been using fitted to implement
List and vector also became a part of Java Collection framework.

All the methods of vector is synchronized. But, the methods of arraylist is not synchronized.All the new implementations of Java Collection framework is not synchronized.

Vector and arraylist both uses array internally as data structure. they are dynamically resizable. difference is in the way they are internally resized. by default, vector doubles the size of its array when its size is increased. but, arraylist increases
By half of its size when its size is increased.

Therefore as per Java API the only main difference is, vector's methods are synchronized and arraylist's methods are not synchronized.

Vector or arraylist? Which is better to use in Java?

In general, executing a 'synchronized' method results in costlier performance than a unsynchronized method. keeping the difference in mind, using vector will incur a performance hit than the arraylist. but, when there is a certain need for thread-Safe Operation
Vector needs to be used.

Is there an alternate available in Java for vector?
Arraylist can be synchronized using the Java collections framework Utility Class and then arraylist itself can be used in place of vector.

When there is no need for synchronized operation and you still look for better performance 'array' can be used instead of arraylist. but the development is tedious, since it doesn't provide user friendly methods.

When you use vector or arraylist, always initialize to the largest capacity that the Java program will need. Since incrementing the size is a costlier operation.

 

Translation:

 

Java. util. Vector already exists since jdk1.0. java. util. arraylist is introduced from jdk1.2 and is part of the collection framework. According to the Java API, the vector has been redefined since jdk1.2. It implements the list interface and the vector has become part of the collection framework.

All the methods of vector are synchronized (Note: All Public Non-overload methods). However, all the methods of arraylist are not synchronized.The new implementation methods of all Java Collection frameworks are not synchronized.

Both vector and arraylist use Arrays for data structures. they all dynamically change the size. the difference is that they change the internal size. by default, the vector size doubles the normal array size. However, the normal arraylist size is half of the normal array size.

Therefore, according to Java API, the main difference between vector and arraylist is that the vector method is synchronized, And the arraylist method is not synchronized.

In Java, Which of the following is better for vector and arraylist?


In general, the synchronized method will cause high performance consumption compared with the synchronized method. keeping the difference in mind (not translated), using vector will consume higher performance than using arraylist. however, when a thread-safe operation is required, vector is necessary.

Is there anything in Java that can replace vector?

Arraylist can use the Java Collection framework tool class to implement synchronized, and then arraylist can replace vector.

When synchronized is not required, you can still find a better implementation to replace arraylist, but this development is boring because it cannot provide user-friendly methods.

When you use vector or arraylist, they are always initialized to the maximum capacity to facilitate the use of Java programs, so increasing its size is an expensive operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.