Chapter 2 holds your object
I. Introduction to the container
1. Classification of containers
1.1. Collection: A set of independent elements, that is, each of which holds only one element.
1) List: Place an element in the order of an element and does not rearrange it.
2) SET: Do not receive a repetition element, it will use one of its own arrangements
1.2. Map: A group of key-value objects, that is, the HEY-VALUE PAIRS.
There is no repetitive key in the map, which has its own internal arrangement mechanism.
2. Element type in the container is Object. When an element is obtained from a container, it must be converted into the original type.
Detailed introduction to the container
1. COLLECTION
Collection does not provide a get () method. If you want to traverse the elements in Collectin, you must use item.
1.1. List
1.1.1 List (interface): List Add some functions to Collectin so that it can make an insertion and removal actions within the LIST. List will generate Listiterator, which can be interviewed from both directions, or can be used in the LIST.
1.1.2 ArrayList: It can be quickly randomly accessed; however, it is very efficient when the settings of the element occurs in the central position of the List. It is not advisable to use ArrayList to make an insertion and removal operation.
1.1.3 LinkedList: In contrast to ArrayList, suitable for installation and removal, but random access is slow. In addition, STACK, Queue, Deque can be implemented via LinkedList.
1) AddFirst (), getfirst (), getFirst (), getFirst (), received (), RemoveList () function is not defined in any Interface or Base Class, so it can only be used in LinkedList.
1.2. Set
1.2.1 Set (Interface): SET has an exact same interface as the Collection (Difference: List joins your own function), so set is a collection, but its behavior is different. Each element of the SET must be unique, no repetition with other elements; SET does not allow repeating elements, each element must define Equals () to determine the so-called uniqueness.
1.2.2 Hashset: A sets that looks a lookup time is very important. All elements must define HashCode ().
1.2.3 Treeset: An ordered SET for the underlying structure for TREE.
2. Map
2.1. Map: Maintain the correlation of Key-Value so that you can use Key to find Value.
1) KeySet () function and values () function
Import java.util. *;
PUBLIC CLASS ExplicitStatic {
Public Static Void PrintKeys (Map M) {
System.out.print ("size =" m.size ());
System.out.println (", Keys:" M.KeySet ());
}
Public Static Void PrintValues (MAP M) {
System.out.println ("VALUES:" M.VALUES ());
}
Public Static Void Test (Map M) {
For (int i = 1; i <10; i )
M.PUT ("km" i, "m" i);
PRINTKEYS (M);
PrintVALUES (M);
System.out.println ("km1 -" m.get ("km1"));
Set keys = m.keyset (); // (1)
Collection Values = m.values (); // (2)
Keys.Remove ("km2"); // (3)
VALUES.Remove ("m1"); // (4)
System.out.println ("km1 -" m.get ("km1"));
PRINTKEYS (M);
PrintVALUES (M);
}
Public static void main (String [] args) {
System.out.println ("Testing Hashmap");
Test (new hashmap ());
}
}
The result is:
Testing hashmap
Size = 9, Keys: [KM5, KM4, KM3, KM2, KM1, KM9, KM8, KM7, KM6]
VALUES: [M5, M4, M3, M2, M1, M9, M8, M7, M6]
KM1 - M1 // Perform (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 is obtained by the keys and values in the map, respectively. From the code before and after execution (3) (4), it is known to modify the value obtained by the keyset () and values () functions.
(3) Remove the value of "km2", (4) deleted the value "m1", and they are the same key-value pair, but the result (5) (6) indicates that MAP The deletion is two Key-Pair. As soon as, as long as one of the key or value in the map is deleted, the entire key-value pair will be deleted.
2) ASDF
2.2. Hashmap: Can insert an element within a constant time, or find a set of Key-Value Pair. Through its constructor, the user can adjust the performance performance because it allows you to set Capacity and LoadFactor (load factor).