Description and difference of collection classes

Source: Internet
Author: User
Collection objects list │ collect objects list │ ├ arraylist │ empty vector │ empty stack using setmap using hashtable using hashmap using weakhashmapcollection Interface collection is the most basic set interface. A collection represents a group of objects, elements of the collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. The Java SDK does not provide classes that directly inherit from collections. The classes provided by the Java SDK are the "subinterfaces" that inherit from collections, such as list and set. 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, and each element in the collection can be accessed one by one using this iterator. Typical usage: iterator it = collection. iterator (); // get an iterator while (it. hasnext () {object OBJ = it. next (); // get the next element}. The two interfaces derived from the collection interface are list and set. The list interface list is an ordered collection, which can be used 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. Unlike the set mentioned below, the list can have the same element. 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. The listlist class implements the list interface, allowing null elements. In addition, the values list provides additional get, remove, and insert methods at the beginning or end of the values list. These operations enable the queue list to be used as a stack, queue, or two-way Queue (deque ). Note that the synchronized list method is not available. If multiple threads access a list at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized list when creating a list: List list = collections. synchronizedlist (New synchronized list (...)); arraylist class arraylist implements an array of variable sizes. It allows all elements, including null. Arraylist is not synchronized. 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. Like the synchronized list, arraylist is also non-synchronous (unsynchronized ). The vector class vector is very similar to 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 stacks inherit from the 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. The 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 the Set interface 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. Note that map does not inherit the collection interface. Map provides the key ing from key to 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. The hashtable class 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. A simple example of hashtable is as follows: Put 1, 2, 3 into hashtable, and their keys are "one", "two", "three": hashtable numbers = new hashtable (); numbers. put ("one", new INTEGER (1); numbers. put ("two", new INTEGER (2); numbers. put ("three", new INTEGER (3); to retrieve a number, such as 2, use the corresponding key: integer n = (integer) numbers. get ("two"); system. out. println ("Two =" + n); because the object as the key is determined by calculating its hash function, therefore, any object as a key must implement the hashcode and equals method. The hashcode and equals Methods inherit from the root class object. If you use a custom class as the key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, if obj1.equals (obj2) = true, their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different. If the hashcode of two different objects is the same, this phenomenon is called a conflict. A conflict will increase the time overhead for operating the hash table. Therefore, the hashcode () method should be defined as much as possible to speed up the operation of the hash table. If the same object has different hashcode, operations on the hash table will produce unexpected results (the expected get method returns NULL). To avoid this problem, you only need to remember one: the equals and hashcode methods must be rewritten at the same time, instead of writing only one of them. Hashtable is synchronous. The hashmap class hashmap is similar to hashtable. The difference is that hashmap 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. 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. To sum up, 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. 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. Synchronous vector is synchronous. Some methods in this class ensure that the objects in the vector are thread-safe. Arraylist is asynchronous, so the objects in arraylist are not thread-safe. Because the synchronization requirements will affect the execution efficiency, it is a good choice to use arraylist if you do not need a thread-safe set, this avoids unnecessary performance overhead due to synchronization. In terms of the internal implementation mechanism of data growth, both arraylist and vector use arrays to control objects in a set. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, both of them need to extend the length of the internal array, by default, vector automatically doubles the length of the original array. arraylist is 50% of the original length, so the space occupied by the set you get is always larger than what you actually need. Therefore, if you want to save a large amount of data in the Set, using vector has some advantages, because you can avoid unnecessary resource overhead by setting the initial size of the set. The usage mode in arraylist and vector queries data from a specified position (through index) or adds or removes an element at the end of the set at the same time, this time is represented by O (1. However, if an element is added or removed from another position in the Set, the time consumed will grow linearly: O (n-I), where N represents the number of elements in the set, I indicates the index location where the element is added or removed. Why? It is assumed that all elements after the I and I elements in the collection must be displaced during the above operations. What does all this mean? This means that you can only search for elements at a specific position or add or remove elements at the end of the set. You can use vector or arraylist. For other operations, you 'd better select another set operation class. For example, does the linklist set class take the same time to add or remove any element from the set? O (1), but it is slow to index an element-O (I), where I is the index position. it is also easy to use arraylist, because you can simply use indexes instead of creating iterator objects. Linklist also creates an object for each inserted element, and you need to understand that it also brings additional overhead. Finally, in practical Java, Peter Haggar recommends using a simple array instead of vector or arraylist. This is especially true for programs with high execution efficiency requirements. Array is used to avoid synchronization, additional method calls, and unnecessary Space reallocation. The difference between vector and arraylist1 is that the vector is thread-synchronized, so it is thread-safe, while the arraylist is asynchronous and insecure. If thread security is not taken into account, arraylist is generally used for high efficiency. 2. 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 that of arraylist is 50% of the current array length. if you use a large amount of data in a collection, using vector has some advantages. 3. If you look for data at a specified position, the time used by the vector and arraylist is the same, both of which are 0 (1). In this case, you can use both vector and arraylist. If it takes 0 (n-I) n to move the data at a specified position as the total length, you should consider using linklist, because it takes 0 (1) to move data at a specified location, and the time spent querying data at a specified location is 0 (I ). Arraylist and vector use arrays to store data. The number of elements in the array is greater than that in the actual storage to add and insert elements. Both allow direct serial number index elements, however, data insertion is designed to move array elements and other memory operations. Therefore, index data is inserted slowly. Because vector uses the Synchronized Method (thread-safe), its performance is inferior to that of arraylist, the sort list uses a two-way linked list for storage. Data indexed by serial number needs to be traversed forward or backward. However, when inserting data, you only need to record the items before and after this item, so the insertion speed is faster! Arraylist and list1.arraylist implement a dynamic array-based data structure. The arraylist is based on the linked list data structure. 2. for Random Access to get and set, arraylist thinks it is better than the sorted list because the sorted list needs to move the pointer. 3. For add and remove operations, linedlist is dominant because arraylist needs to move data. This depends on the actual situation. If only a single piece of data is inserted or deleted, the arraylist speed is better than the sort list speed. However, if data is inserted and deleted randomly in batches, the speed of the sorted list is much higher than that of the arraylist. Because each inserted data in the arraylist needs to move the inserted point and all subsequent data. Hashmap and treemap (Note) Article Source: http://www.diybl.com/course/3_program/java/javaxl/200875/130233.html 1. hashmap through hashcode to quickly find its content, and all the elements in treemap maintain a fixed order, if you need to get an ordered result, you should use treemap (the arrangement order of elements in hashmap is not fixed ). The order of elements in hashmap is not fixed ). 2. hashmap uses hashcode to quickly search its content, while all elements in treemap maintain a fixed order, if you need to get an ordered result, you should use treemap (the arrangement order of elements in hashmap is not fixed ). The Collection framework provides two general map implementations: hashmap and treemap (the sortedmap interface implemented by treemap ). 3. insert, delete, and locate elements in map. hashmap is the best choice. 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. This treemap has no optimization options because the tree is always in the balance state. After research, I also found that, based on the original author, the two-tree map is the same, but the order is different, resulting in different hashcode. Perform the same test: In hashmap, the map of the same value has different order. For equals, the map of the same value is false. For treemap, the map of the same value has different order. For equals, true indicates that treemap sorted the order in equals. Hashtable and hashmap 1. historical Reasons: hashtable is based on the obsolete dictionary class, And hashmap is an implementation of the map interface introduced by Java 1.2. synchronization: hashtable is thread-safe, that is, synchronous, while hashmap is not secure for line programs, not synchronous. value: Only hashmap allows you to use a null value as the key or value of a table entry.

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.