Collection: List, Set Map: Hashmap, hashtable How to select between them I. array, Arrays Array is the most efficient method for Java's "Storage and random access to a series of objects. 1, High efficiency, but the capacity is fixed and cannot be changed dynamically. Another disadvantage of array is that it cannot determine the actual number of elements in the array. length only tells us the array capacity. 2. There isArrays class, used to operate the Array. Arrays has a set of static functions, Equals (): checks whether two arrays are equal. Array has the same number of elements, and all corresponding elements are equal to each other. Fill (): Enter the value in array. Sort (): used to sort arrays. Binarysearch (): Search for elements in the sorted array. System. arraycopy (): copying an array. Ii. Collection and map If you do not know how many objects are needed when writing a program and need to automatically expand the capacity when the space is insufficient, you need to use the container class library. array is not applicable. 1. Differences between collection and map The number of elements stored in each container is different. Collection type, each location has only one element. Map type, holding key-value pair, like a small database. 2. subcategories Collection -- List: stores elements in a specific order. Therefore, the order obtained may be different from the order placed. -- Arraylist/vector list/vector -- Set: duplicate elements cannot be contained. -- Hashset/treeset Map -- Hashmap -- Hashtable -- Treemap 3. Other features * List, set, and map objects are regarded as object types. * Collection, list, set, and map are all interfaces and cannot be instantiated. The arraylist, vector, hashtable, and hashmap inherited from them are concrete classes, which can be instantiated. * The vector container knows exactly the type of the object it holds. Vector does not perform boundary check. Iii. Collections Collections is a help class for the Collection class.Provides a series of static methods for searching, sorting, and thread-based operations on various sets. It is equivalent to arrays, a class that performs similar operations on arrays. For example, collections. Max (Collection Coll); obtains the largest element in Coll. Collections. Sort (list); sorts the elements in the list.
4. How to choose? 1. Differences between the container class and array * The container class can only hold the object reference (pointer to the object), instead of copying the object information to a certain position in the series. * Once an object is placed in a container, the type information of the object is lost. 2, * Among various lists, it is best to use arraylist as the default choice. Use the sort list () When insertion or deletion is frequent (); Vector is always slower than arraylist, so avoid using it whenever possible. * In various sets, hashset is generally better than hashtree (insert and search ). Treeset is used only when a sorted sequence is generated. The only reason for the existence of hashtree is that it can maintain the sorting status of its elements. * In various maps Hashmap is used for quick search. * When the number of elements is fixed, array is used because the array efficiency is the highest. Conclusion: arraylist, hashset, hashmap, and array are commonly used. Note:
1. collection does not have the get () method to obtain an element. You can only use iterator () to traverse elements. 2. Set and collection have the same interface. 3. list. You can use the get () method to retrieve an element at a time. Use numbers to select one of a bunch of objects, get (0 ).... (Add/get) 4. arraylist is generally used. Use the queue list to construct the stack and queue. 5. Map uses put (K, v)/get (K) and containskey ()/containsvalue () to check whether a key/value exists. Hashmap uses the hashcode of the object to quickly find the key. * Hashing The hash code transforms the object information to form a unique int value, which is stored in an array. We all know that the array search speed is the fastest in all storage structures. Therefore, the search can be accelerated.
When a collision occurs, let the array point to multiple values. That is, an orders table is generated at each position of the array. 6. Elements in map can be extracted separately from the key sequence and value sequence. Use keyset () to extract the key sequence and generate a set for all the keys in the map. Use values () to extract the value sequence and generate a collection for all values in the map. Why does one generate a set and one generate a collection? That's because the key is always unique and the value can be repeated. Collection list set map difference memory all represent the set in Java. Here, we mainly perform difference memory based on whether the elements are ordered and whether they can be reduplicated for proper use, of course, there are still differences in synchronization. See the previous article.
|
Ordered or not |
Allow repeated Elements |
Collection |
No |
Yes |
List |
Yes |
Yes |
Set |
Abstractset |
No |
No |
Hashset |
Treeset |
Yes (Binary Tree sorting) |
Map |
Abstractmap |
No |
Use Key-value to map and store data. The key must be unique and the value can be repeated. |
Hashmap |
Treemap |
Yes (Binary Tree sorting) |
The list interface is used to expand the collection. Its specific implementation classes are commonly used arraylist and javaslist. You can put everything in a list container and retrieve it as needed. Arraylist can be seen from its name that it is stored in an array-like form, so its random access speed is extremely fast, while the internal implementation of the List is a linked list, it is suitable for frequent insert and delete operations in the center of the linked list. You can select an application as needed. The iterator mentioned above can only traverse the container forward, while the listiterator inherits the iterator idea and provides a bidirectional Traversal method for the list.The Set interface is also an extension of the collection, but different from the list, the object elements in the set cannot be repeated, that is, you cannot put the same thing twice into the same set container. It is often used to implement hashset and treeset classes. Hashset can quickly locate an element, but the object you put into hashset needs to implement the hashcode () method, which uses the hash code algorithm mentioned above. Treeset stores the elements in the Set in order, which requires that the objects in the set are sortable. This uses the other two aggregation classes comparable and comparator provided by the set framework. A class can be sorted, and it should implement the comparable interface. If multiple classes have the same sorting algorithm, you do not need to define the same Sorting Algorithm for each class. You only need to implement the comparator interface. There are two useful public classes in the Collection framework: collections and arrays. Collections provides some useful methods for sorting, copying, searching, and filling a collection container. arrays performs similar operations on an array. Map is a container that associates key objects and value objects, and a value object can be a map, and so on. In this way, a multi-level ing can be formed. For key objects, similar to set, key objects in a map container are not allowed to be repeated to maintain consistency of the search results. If there are two key objects, if you think about the value object corresponding to the key object, it will be wrong. Maybe what you get is not the value object you want, and the result will be messy, therefore, the uniqueness of keys is very important and also conforms to the set nature. Of course, during use, the value object corresponding to a key may change. At this time, the modified value object will correspond to the key. There is no uniqueness requirement for value objects. You can map any key to a value object without any problem (however, it may cause inconvenience to your use, you don't know whether you get the value object corresponding to that key ). MAP has two common implementations: hashmap and treemap. Hashmap also uses the hash algorithm to quickly find a key. treemap stores the key in order, so it has some extended methods, such as firstkey (), lastkey (), etc. You can also specify a range from the treemap to obtain its submap. The association between keys and values is very simple. You can associate a key with a value object using the pub (Object key, object Value) method. You can use get (Object key) to obtain the value object corresponding to this key object. Address: http://blog.chinaunix.net/u2/65993/showart_526833.html |