Java Collection and array
1. Java2 provides a complete collection framework, while earlier Java versions only provide some collection classes, such as hashtable and vector, and built-in array functions.
2. the classes and interfaces that constitute the collection framework are Java. A member of the util package. The arrays class provides the static method used to operate arrays. The method of the initrys class includes the binarysearch Method Used for search and the equals method used to compare arrays, the fill method used to assign values to the array, and the sort and aslist methods for sorting. In addition, objects in the arrays class are sorted. objects must implement the compareto method of the comparable interface.
4. the collection interface is the Root Interface in the collection hierarchy. Both the set interface and the list interface are derived from the collection interface. The collection interface includes adding, clearing, and, compare and maintain large-scale operations on objects. The collection interface provides the iterator method to get iterator.
5. The collections class provides static methods for many operation sets, many of which are used for searching and sorting.
6. list is a sorted collection that can contain repeated elements. Sometimes, list is also called a sequence. The list interface is implemented by the arraylist class, sorted list class, and vector. arraylist is the implementation of a Variable list array, similar to the behaviors and functions of the Vector class, the listlist is the linked list implementation.
7. Does the hasnext method collection of iterator contain another element? If there are other elements, the hasnext method returns true, and the next method returns the next object in the collection and moves it to the next element.
8. sublist gets a part of the list. This part is called a sub-Table. Modifications to the sub-table will affect the list. The clear method deletes elements from the list,
9. The toarray method returns the content in the set as an array.
10. The comparator object provides a sorting method to sort collection elements in the order of different colletions. The reverseorder method returns a comparator object, which represents the reverse sorting of the set. You can also implement your own sorting method on the premise that the comparator interface is implemented and the compare method of the comparator interface is implemented.
11. The shuffle algorithm sorts the elements in the list randomly. The binarysearch algorithm searches for an object in the list.
12. set is a collection that does not contain duplicate elements. The collection framework contains two implementation methods of set: hashset and treeset. hashset stores its elements in the hash table, treeset stores its elements in the tree.
13. The sortedset interface extends the set and maintains its elements in the arranged order. The treeset class implements it.
14. the headset method obtains a treeset that is smaller than a specified subset of elements. modifications to this subset are reflected in the treeset. The tailset method obtains a subset that is greater than or equal to the specified one, similarly, all modifications to it are reflected in treeset.
15. map maps keys to values that do not contain duplicate keys. The difference between map and set is that map contains keys and values, while set only contains keys. The hashmap and treemap classes implement the map interface, hashmap stores its elements in the hash table, while treemap stores its elements in the tree.
16. The sortedmap interface extends the map and maintains its elements in the arranged order. The treemap class implements it.
17. the built-in collection is asynchronous, and concurrent access to collection by multiple threads may lead to uncertain results. To prevent this problem, you can use the synchronous packaging class outside the class accessed by multiple threads, the collections class provides a series of static methods to convert a set into a form that cannot be modified. An exception is thrown when an attempt is made to modify the set.
Public static method header file: (synchronous)
Collection synchronizedcollection (collection C );
List synchronizedlist (List C)
Others are similar ..
Public static method header file: (cannot be modified)
Collection unmodifiablecollection (collection C );
List unmodifiablelist (List C)
Others are similar ..
The Collection framework provides different abstract implementations. programmers can use this to implement their own defined implementations!
18. There are three main differences between arrays and containers: efficiency, type recognition, and the ability to hold primitives
19. the types maintained by the container class are all object. There are mainly three types of list, set, map, and container. The disadvantage is that when elements are saved, the element type is lost. When elements are used, type conversion is required.
20 list selection:
The random access efficiency of arraylist is faster than that of linklist, but the sequential access efficiency of linklist is higher than that of arraylist. In addition, linklist is more efficient than arraylist in inserting and deleting elements, the best way to choose the two is: first, select arraylist. When we find that "too many insert and delete operations to the element" causes performance problems, use the linklist instead. Of course, we should choose an array to process fixed elements.
21. Set selection:
Hashset has better performance than treeset, but treeset can save elements sequentially. Therefore, treeset is used only when sequential set is required;
The insert operation of the linkedhashset is slower than that of the hashset, mainly because the former bears the double cost of the linked list and the hash container, and because of the linked list, the traversal of the linkedhashset is faster than that of the hashset.
22. Map selection:
Hashmap is similar to hashtable. Generally, hashmap is faster. hashmap is used to replace hashtable. treemap is less efficient than hahsmap, but treemap is ordered because it is a tree structure, therefore, sorting is not required. If you do not need a map with a constant order, hashmap is generally preferred. Slow hashmap is slower than hashmap because besides saving the hash container, it also saves the linked list, identityhashmap cannot be compared with the preceding map. It uses = to compare the equality of two objects, instead of equals ()
23. All objects held by hash containers must overwrite the hashcode and equals methods.