The following is an inheritance relationship diagram of the collection type in the downloaded Java to facilitate correct understanding and use of the corresponding collection type.
| |
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) |
Several interview Frequently Asked Questions:
1. Q: What is the difference between arraylist and vector? What is the difference between hashmap and hashtable?
A: The vector and hashtable are synchronized ). In terms of performance, arraylist and hashmap are better than vector and hashtable.
2. Q: the architecture of the Java set
A: List, set, and map are the three most important interfaces in this collection system.
List and set are inherited from the collection interface.
Set does not allow repeated elements. Hashset and treeset are two main implementation classes.
The list is ordered and repeated elements are allowed. Arraylist, vector list, and vector are three main implementation classes.
MAP also belongs to the collection system, but it is different from the collection interface. Map is a set of key-to-value ing, where the key column is a set. The key cannot be repeated, but the value can be repeated. Hashmap, treemap, and hashtable are three main implementation classes.
The sortedset and sortedmap interfaces sort elements by specified rules. sortedmap sorts key columns.
3. Q: What is the difference between comparable and comparator?
A: When you call java. util. Collections. Sort (list List) to sort objects, all objects in the list must implement the comparable interface.
Java. util. Collections. Sort (list, comparator c). A comparator can be declared temporarily for sorting.
Collections. Sort (imagelist, new comparator (){
Public int compare (Object A, object B ){
Int ordera = integer. parseint (image) a). getsequence ());
Int orderb = integer. parseint (image) B). getsequence ());
Return ordera-orderb;
}
});
If you want to change the order
Change return orderb-ordera.
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, and the internal implementation of the arraylist 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. Its common implementations include the hashset and treeset classes. Hashset can quickly locate an element, but you need to implement the hashcode () method for the objects you put into the hashset. It 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, the problem occurs when you want to get the value object corresponding to the key object. 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 number of keys to a value object without any problems (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.
Key, object Value) method to associate a key with a value object. You can use get (Object key) to obtain the value object corresponding to this key object.
Map traversal:
// The most common Traversal method. The most common method is the most commonly used method. Although it is not complex, it is very important. This is what we are most familiar with. I will not talk about it much !! Public static void work (Map <string, student> map) {collection <student> C = map. values (); iterator it = C. iterator (); For (; it. hasnext ();) {system. out. println (it. next () ;}/// use keyset for traversal. The advantage of this function is that you can set the value of the desired value based on the key, making it more flexible !! Public static void workbykeyset (Map <string, student> map) {set <string> key = map. keyset (); For (iterator it = key. iterator (); it. hasnext ();) {string S = (string) it. next (); system. out. println (map. get (s) ;}/// a more complex Traversal method ~~ He is very violent. He is so flexible that he can get whatever he wants ~~ Public static void workbyentry (Map <string, student> map) {set <map. entry <string, student> set = map. entryset (); For (iterator <map. entry <string, student> it = set. iterator (); it. hasnext ();) {map. entry <string, student> entry = (map. entry <string, student>) it. next (); system. out. println (entry. getkey () + "->" + entry. getvalue () ;}// map. another concise method of entry (foreach traversal) public static void workbyentry (Map <string, student> map) {set <map. entry <string, student> set = map. entryset (); For (map. entry <string, student> me: Set) {system. out. println (Me. getkey () + "->" + me. getvalue ());}}