Chapter 4 hold your object
1. Introduction to containers
1. container classification
1.1. Collection: A group of independent elements, that is, each position in the group holds only one element.
1) List: the elements are placed in the order of element insertion and will not be rearranged.
2) Set: it does not like repeated elements. It uses its own internal arrangement mechanism.
1.2. Map: a group of key-value objects holding key-value pairs.
A Map cannot contain duplicate keys. It has its own internal arrangement mechanism.
2. The element types in the container are all objects. When getting an element from a container, you must convert it to the original type.
Ii. Container details
1. Collection
Collection does not provide the get () method. To traverse the elements in Collectin, you must use Iterator.
1.1. List
1.1.1 List (interface): List adds some functions to Collectin so that it can be inserted and removed from the List. The ListIterator is generated by List, which can be used to access the List in two directions, or to insert and remove elements in the List.
1.1.2 ArrayList: quick and random access. However, it is inefficient to insert or remove an element at the central position of the List. It is not advisable to use ArrayList for the insert and remove operations.
1.1.3 reverse list: opposite to ArrayList, it is suitable for inserting and removing, but the random access speed is slow. In addition, stack, queue, and deque can be implemented through the consumer list.
1) The addFirst (), addLast (), getFirst (), getLast (), removeFirst (), and removeLast () functions in the detail list are not defined in any interface or base class, therefore, it can only be used in the listing list.
1.2. Set
1.2.1 Set (interface): Set has the same interface as Collection (difference: List adds its own function), so Set is a Collection, but its behavior is different. Each element added to the Set must be unique and not repeated with other elements. Set does not allow repeated elements. Each element must define equals () to determine the uniqueness.
1.2.2 HashSet: a set that determines the search time. All elements must define hashCode ().
1.2.3 TreeSet: an ordered Set with the underlying structure of tree.
2. Map
2.1. Map: maintain the relevance of the key-value so that you can use the key to find the value.
1) KeySet () and values () Functions
- ImportJava. util .*;
- Public ClassExplicitStatic {
- Public Static VoidPrintKeys (MapM ){
- System. Out. print ("Size =" + m. size ());
- System. Out. println (", Keys:" + m. keySet ());
- }
- Public Static VoidPrintValues (MapM ){
- System. Out. println ("Values:" + m. values ());
- }
- Public Static VoidTest (MapM ){
- For(IntI = 1; I <10; I ++)
- M. put ("km" + I, "m" + I );
- PrintKeys (m );
- PrintValues (m );
- System. Out. println ("km1-" + m. get ("km1 "));
- SetKeys = m. keySet ();// (1)
- CollectionValues = m. values ();// (2)
- Keys. remove ("km2 ");// (3)
- Values. remove ("m1 ");// (4)
- System. Out. println ("km1-" + m. get ("km1 "));
- PrintKeys (m );
- PrintValues (m );
- }
- Public Static VoidMain (String[] Args ){
- System. Out. println ("Testing HashMap ");
- Test (New HashMap());
- }
- }
Result:
Testing HashMap
Size = 9, Keys: [km5, km4, km3, km1, km1, km9, km8, km7, km6]
Values: [m5, m4, m3, m2, m1, m9, m8, m7, m6]
Km1-m1 // before execution (3) (4)
Km1-null
Size = 7, Keys: [km5, km4, km3, km9, km8, km7, km6] // (5)
Values: [m5, m4, m3, m9, m8, m7, m6] // (6)
At (1) (2), the code obtains the keys and values in Map. We can see from the code before and after execution (3) (4) that modifying the values obtained through the keySet () and values () functions reflects the Map itself.
(3) the key with the value of "km²" is deleted, (4) the value with the value of "m1" is deleted, and they are the same key-value pair, however, the result (5) (6) indicates that two keys-pair are deleted in Map. Slave