4.4 General Collection Library
1.The Collection API
script>
A Collection is an object that contains a set of Object objects. The Collection API contains 3 interfaces that organize the Object to: Collection: A group of Objects, no special order, allowing repeating elements. . List: You must accommodate the elements in a specific order, allowing the elements to be repeated. A set (set): There is no special order, which cannot contain any repetitive elements. See example: setExmple.javalistexmple.java
2.map
script>
. Map maintains the "key-value" correspondence (pair) to find the corresponding value through a key. HashMap is implemented based on a hash table (with it instead of HashTable). Insert and retrieve the "Key-value" pair, this form has the most stable performance. See Questions: PrintingContainers.java Plunches Containers: You can only sET or Map See Example: FillingLists.javacontainer Disadvantages: Don't know the data type. You must do your own type conversion Example: cat.javadog.javacatsandDogs.java Compare List, LinkedList ArrayListList (interface) order is the most important feature of List:. Elements are arranged in the specified order. .List Added a large number of methods for Collection: Insert and delete elements in the middle of the List. (Listiterator), use it to travel in two directions in one list while inserting and deleting elements located in the middle of the list Compare LIST , LinkedList, ArrayList: Allows us to quickly access the elements, but when inserting and deleting elements from the middle of the list, the speed is too slow. Generally, you should only use Listiterator to forward and backward traversal, don't use it to delete it. And insert the element; LinkedList: Insert and delete the operation in the middle of the list efficiently. However, when random access is performed, addFirst (), addlast (), getFirst (), getlast (), removefirst ( ) And removelast () (not defined in any interface or foundation) to use the LinkedList to implement the queue (QueueuE) operation as a specification, queue, and a two-way queue. See Example: Queue.JavaMap Comprehensive Example: Map1. The Java array Arrays class provides an overloaded sort () and binarysearch () for all the basic data types, which can also be used for String and Object. See example: array1.java
3. Reproduction
script>
Iterators Reproduction: Repeat is used to extract elements in the collection. An Iterator of A Set is disorderless. Listiterator of a List can traverse mobile Iterators repetitions: See example: CatsandDogs2.javahamstermaze.java See Iterators Reproduction and Collection's Comprehensive Example: Collection1.java4. Sort and Search
script>
A list (list) can be sorted and searched in the same form as an array. The static method for sorting and searching lists is included in class collections, sort (list) is used to sort a list of objects that implement Comparable; binarysearch (list, object) array elements can be basic data types and reference data types. The array element type is clear. Can be multi-dimensional. The disadvantage is that once it is created, it cannot be changed. Colloction stores a set of individual elements, while the MAP stores a set of elements to store the LIST ordered an ordered element, but does not store the basic data type, and require type conversion when reading the element. ArrayList is used to randomly read data. LinkedList is used to insert a lot of data from the middle. LinkedList is used to implement queue and stack MAP for connecting a pair of Object-Object. Hashmap is used to quickly find it. . SET cannot accept repeating elements. Hashset is used to provide fast look.
5.collections class
script>