I. The difference between an array and a set
Arrays and collections are containers for storing objects, but arrays can only store objects of the base data type , and the length is immutable , and the collection is the object that stores the reference data class , and the length is variable , so A collection is typically used to store a specific number of objects without knowing the value of the object.
Two. Level diagram under the Javase stage
From the level graph can be seen, the collection class is implemented by the interface, where <E> and <K,V> is generic, simply speaking, the generic is the referenced object type, the collection is mainly divided into single-column sets and double columns set, iterator iterator is only the use of the collection, The iterator interface is implemented to iterate through the interfaces of the elements in the collection, mainly using unordered sets and enhanced for loops.
Three. Iterator iterators and enhanced for loops
The 1.Iterator interface mainly consists of three methods: ①.hasnext ();//Whether there is the next element, ②.next ();//returns the next element; ③.remove ();//Deletes the current element.
2. Enhance the For loop:
①. New features from the jdk1.5 that function to simplify the writing format of iterators (note: Enhancing the bottom of a for loop or using an iterator traversal.) ), an object that implements the Iterable interface, or an array object;
②. Enhancing the format for the For Loop, for (data type variable name: traversed target) { }
③. Cautions:
- The enhanced for loop is also obtained using an iterator, except that the acquisition iterator is done by the JVM and does not require us to obtain an iterator, so the collection object is not allowed to modify the number of elements in the collection when using the enhanced for loop variable element.
- When you enhance an element of a for loop variable collection, you cannot call the Remove method of the iterator to delete the element;
- The enhanced for loop must have a traversal target.
Four. Single-column collection collection and Collections tool class
1.List (ordered, indexed, repeatable)
List of objects can be arranged in a certain order, so the query speed, but in addition or deletion, will make the corresponding movement of the subsequent data, so it will become slow, the common list set has ArrayList set and so on.
2.Set(unordered, non-indexed, non-repeatable)
Set and list set is the opposite, and thus is unordered, so the query is slow, on the contrary, the deletion is faster, the common set set has HashSet set and so on.
3.Collections Tool Class
The Collections Tool Class simply uses the collection interface, all subclasses or sub-interfaces can inherit the tool class, using the method inside to simplify the implementation of the method.
Five. Double-row Set map
1.Map and its hashmap,hashtable
Map and HashMap use consistent, both are unordered, non-repeatable, its key (K) can not be repeated, the value (V) may be repeated, according to the key to get the value; HashMap is the most commonly used map, which stores data according to the hashcode value of the key, has fast access speed, The order in which the data is obtained is completely random. Because the key object cannot be duplicated, HashMap allows a maximum of one record's key to be null, allows multiple records to be NULL, is non-synchronous, and Hashtable, like HashMap, is a thread-safe version of HashMap, which supports thread synchronization , that is, at any one time only one thread can write Hashtable, which also causes the Hashtale to write slower, it inherits from the dictionary class, except that it does not allow the record key or value to be null, while the efficiency is low.
Two ways to traverse 2.MAP
①.KeySet ()
Deposits all the keys in the map into the set collection. Because set has iterators. All of the keys can be iterated out and then based on the Get method. Gets the value corresponding to each key. KeySet (): The key can only be taken with get () after the iteration.
The result is random, because the Hashmap.keyset () method is used when the primary key of the data row is obtained, and the result of the set returned by this method is that the data is emitted in a disorderly order.
Typical usage is as follows:
map<string,string> map = new hashmap<string,string> ();
Map.put ("Key1", "value1");
Map.put ("Key2", "value2");
Map.put ("Key3", "value3");
set<string> keys = map.keyset ();//Gets the set set of all keys for the map collection, KeySet ()
Iterator<string> it =keys.iterator ();
Get iterators
while (It.hasnext ()) {
String key = It.next ();
String value = Map.get (key);
Print key and value values
System.out.println (key+ "=" +value);
}
②. entryset ()
set<map.entry<k,v>> entryset () returns the Set view of the mappings contained in this map. is to store (key,value) as a whole pair of pairs in the set set. Map.entry represents a mapping relationship. EntrySet (): After the iteration you can entry . GetKey (), entry . GetValue () Two methods to take key and value. The entry interface is returned, . The typical usage of
is as follows:
map<string,string> Map = new hashmap< String,string> ();
Map.put ("Key1", "value1");
Map.put ("Key2", "value2");
Map.put ("Key3", "value3");
Set<map.entry<string,string>>entrys = Map.entryset ();//Gets the set set of all keys for the Map collection first, EntrySet ()
Iterator< map.entry<string,string> > it = entrys . Iterator ();
//Get iterators
while (It.hasnext ()) {
map.entry<string,string> entry = It.next ();
String key =Entry.getkey ();
String value = entry.getvalue ();
System.out.println ("key:" +key + ", Value:" + value ");
}
Collection class rollup based on the javase phase