The following describes the frequently used collection classes. This section does not introduce how to use the collection classes. It only describes the usage and features of each collection class, then, by comparing different features of related collection classes, we can gain a deeper understanding of them.
Collection Interface
- Collection is the most basic collection interface. A collection represents a group of objects, namely, elements of the collection ).
- All classes that implement the collection interface must provide two standard constructor: A non-parameter constructor is used to create an empty collection, A constructor with the collection parameter is used to create a new collection, which has the same elements as the imported collection. The next constructor allows you to copy a collection.
- How to traverse every element in the collection? Regardless of the actual type of collection, it supports an iterator () method. This method returns an iterator that can be used to traverse each element in the collection,The iterator traversal is unordered..
The typical usage is as follows:
Iterator it = collection. iterator (); // obtain an iterator
While (it. hasnext ()){
Object OBJ = it. Next (); // obtain the next element.
}
List Interface
- List is an ordered collection.You can use this interface to precisely control the insert position of each element. You can use an index (the position of an element in the list, similar to an array subscript) to access the elements in the list, which is similar to an array in Java.
- In addition to the iterator () method required for the collection interface, list also provides a listiterator () method to return a listiterator interface. Compared with the standard iterator interface, listiterator has some more add () you can add, delete, and set elements to traverse forward or backward.
- Common classes that implement the list interface include the list, arraylist, vector, and stack.
Sort list class
- The listlist interface allows null elements. In addition, the writable list provides additional get, remove, and insert methods. These operations enable the queue list to be used as a stack, queue, or two-way Queue (deque ).
- Note that the synchronized list is not synchronous.. If multiple threads access a consumer list at the same time, they must implement access synchronization by themselves. Another solution is to construct a synchronous list when creating the list: List list = collections. synchronizedlist (new collections list (...));
Arraylist class
- Arraylist implements an array of variable sizes. It allows all elements, including null. Arraylist has no synchronization.
- Size, isempty, get, set method running time is constant. However, the overhead of the add method is the constant of the allocation. It takes O (n) to add n elements. The running time of other methods is linear.
- Each arraylist instance has a capacity, that is, the size of the array used to store elements. This capacity can automatically increase with the addition of new elements, but the growth algorithm is not defined. When a large number of elements need to be inserted, you can call the ensurecapacity method before insertion to increase the arraylist capacity to improve the insertion efficiency.
Vector
The vector is very similar to the arraylist, but the vector is synchronized. Although the iterator created by vector is the same interface as the iterator created by arraylist, because vector is synchronous, when an iterator is created and in use, another thread changes the state of the vector (for example, adding or deleting some elements). When calling the iterator method, concurrentmodificationexception is thrown. Therefore, this exception must be caught.
Stack
Stack inherits from vector to implement a post-import, first-out stack. Stack provides five additional methods to make the Vector used as a stack. The basic push and pop methods also include the elements of the peek method to get the top of the stack. The empty method tests whether the stack is empty. The search method checks the position of an element in the stack. The stack is empty after being created.
Comparison of vector, arraylist, and rule list
- 1. vector is thread-synchronized, so it is thread-safe, while arraylist and history list are non-thread-safe. If thread security is not taken into account, arraylist and history list are generally used for high efficiency.
- 2. arraylist and vector implement a dynamic array-based data structure. The arraylist is based on the linked list data structure.
- 3. if the number of elements in the set is greater than the length of the current set array, the growth rate of vector is 100% of the current array length, and the growth rate of arraylist is 50% of the current array length. if a large amount of data is used in a collection, the use of vector has certain advantages; otherwise, the use of arraylist has advantages.
- 3. IfSearchFor data at a specified position, the time used by the vector and arraylist is the same, and the time consumed is O (1). the time consumed by the sorted list is O (I ), it is less efficient than the previous two.
- 4. IfMove and deleteThe time spent on data at a specified position is 0 (n-I) N as the total length. In this case, we should consider using the sort list, because it takes 0 (1) to move data at a specified position ).
- 5.InsertData and linedlist are dominant because arraylist needs to move data.
Set Interface
- Set is a collection that does not contain repeated elements, that is, the two elements E1 and E2 both have e1.equals (E2) = false, and set has a maximum of null elements.
- Obviously, the set constructor has a constraint that the imported collection parameter cannot contain repeated elements.
- Note: You must be careful when operating mutable objects ). If a variable element in a set changes its state, object. Equals (object) = true may cause some problems.
Map Interface
Note that map does not inherit the collection interface. Map provides the key ing between key and value. A map cannot contain the same key, and each key can only map one value.
The map interface provides three sets of views. The map content can be treated as a set of keys, a set of values, or a set of key-value ing.
Hashtable class
- Hashtable inherits the map interface and implements a key-value ing hash table.Any non-null object can be used as a key or value..
- Put (Key, value) is used for adding data, and get (key) is used for retrieving data. The time overhead of these two basic operations is constant.
- Hashtable uses the initial capacity and load factor parameters to adjust the performance. Generally, the default load factor 0.75 achieves a better balance between time and space. Increasing the load factor can save space, but the corresponding search time will increase, which affects operations such as get and put.
- As the key object is determined by calculating its hash function, any object used as the key must implement the hashcode and equals methods. The hashcode and equals Methods inherit from the root class object.
- Hashtable is synchronous.
Hashmap class
Hashmap is similar to hashtable. The difference is thatHashmap is non-synchronous and allows null, that is, null value and null key.. However, when hashmap is treated as a collection (the values () method can return the collection), its iteration suboperation time overhead is proportional to the capacity of hashmap. Therefore, if the performance of iterative operations is very important, do not set the hashmap initialization capacity too high or the load factor too low.
Treemap class
- Hashmap uses hashcode to quickly search and unordered its content. All elements in treemap maintain a fixed and ordered order.
- Hashmap is the best choice for inserting, deleting, and locating elements in a map. However, if you want to traverse keys in the natural or custom order, it is better to use treemap. The key classes required to be added using hashmap clearly define the implementation of hashcode () and equals.
- Treemap has no optimization option because the tree is always in the balance state.
Weakhashmap class
Weakhashmap is an improved hashmap that implements "weak references" to keys. If a key is no longer referenced by external entities, it can be recycled by GC.
Summary
- If operations such as stacks and queues are involved, you should consider using the list. For elements that need to be inserted and deleted quickly, you should use the sort list. If you need to quickly access elements randomly, you should use the arraylist.
- If the program is in a single-threaded environment or the access is only performed in one thread, the efficiency of non-synchronous classes is high. If multiple threads may operate on one class at the same time, synchronous classes should be used.
- Pay special attention to the operations on the hash table. The equals and hashcode methods should be correctly rewritten as the key object.
- When using map, it is best to use hashmap or hashtable for searching, updating, deleting, and adding new map; To perform natural sequence or custom Key sequence of map, it is best to use treemap;
- Try to return the interface rather than the actual type. For example, if the list is returned rather than the arraylist, the client code does not need to be changed if you need to replace the arraylist with the explain list later. This is for abstract programming.
References: http://blog.csdn.net/softwave/article/details/4166598