Vector, enumeration, ArrayList, Collection, Iterator, set, List and other collection class interfaces

Source: Internet
Author: User

In Java programming, it is common to use vector, enumeration, ArrayList, Collection, Iterator, set, List and other collection class interfaces.

1. Vector class and enumeration interface
The vector class is an advanced data structure provided by the Java language that can be used to hold a series of objects, Java does not support arrays, and the vector class provides a function similar to "dynamic arrays". The vector class is a good choice if we cannot predetermine the number of objects to be saved, or if we need to make it easy to get the location of an object.

2.Collection interface and Iterator interface
The collection interface is used like a vector class, except that the name of the method differs. We want to take out all the objects that are stored in the implementation of the collection interface object, and we must also return a iterator interface object through the Collection.iterator method, which is very similar to the function of the enumeration interface with the iterator interface. The JAVA2 platform's data structure Class designer could have expanded the enumeration interface without creating a new interface for iterator. But they do not like the lengthy name of the enumeration interface method, thus creating a new interface for iterator and shortening the length of the method name.
According to the Java syntax, the object cannot be created directly with the collection interface class, and the object must be created with a class that implements the collection interface, and the ArrayList class is a class that implements the collection interface. Instead of using the vectors and enumeration examples above, we will write them in ArrayList and Iteartor to understand the relationship and usage of these classes.

All methods in the ArrayList class are asynchronous, so in the absence of multi-threading security problems, it is better to use ArrayList and the program will be more efficient. In the thread security issue, and our program does not deal with itself (processing refers to the call ArrayList code or method plus synchronous processing), only with the vector.

2. Comparison of collection class interfaces
There are also several collection class interfaces set, List, below which are the collection and their comparisons.
Collection----objects do not have a specified order, allowing repeating elements.
Set----objects do not have a specified order, and duplicate elements are not allowed.
Lists----objects in a specified order, allowing repeating elements.

Vector, enumeration, ArrayList, Collection, Iterator, set, List and other collection class interfaces

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.