------- <A href = "http://www.itheima.com" target = "blank"> android training </a>, <a href = "http://www.itheima.com" target = "blank"> java training </a> we look forward to communicating with you! --------- Generic: New features will appear in the JDK1.5 version. It is a type of security mechanism used to solve security problems. Benefit 1. Shift ClassCastException during runtime to compilation ., It is convenient for programmers to solve problems. Reduce running problems and ensure security. 2, avoiding the trouble of forced conversion. Generic Format: Use <> to define the data type to be referenced. When can I write generics when I use objects provided by java? It is usually common in the Collection framework. As long as you see <>, You need to define the generic type. In fact, <> is used to receive type. When using a set, pass the data type to be stored in the set as a parameter to <>. // Generic class. /* When to define a generic class? When the referenced data types to be operated in the class are uncertain, the Object is defined in the early stage to complete the extension. Now define the generic type to complete the extension. */Class Utils <QQ> {private QQ q; public void setObject (QQ q) {this. q = q ;} public QQ getObject () {return q ;}} class GenericDemo3 {public static void main (String [] args) {Utils <Worker> u = new Utils <Worker> (); u. setObject (new Student (); Worker w = u. getObject ();/* Tool t = new Tool (); t. setObject (new Student (); Worker w = (Worker) t. getObject (); */}: generic type defined by the generic class, which is valid throughout the class. If it is used by methods, all the types to be operated will be fixed after the object of the generic class is clear about the specific type to be operated. Class Demo <T> {public void show (T t) {System. out. println ("show:" + t);} public void print (T t) {System. out. println ("show:" + t) ;}} in order to allow different methods to operate on different types, and the type is still unknown. Then you can define generics on methods. Special: static methods cannot be generic defined on the generic class. If the data type of the application operated by the static method is unknown, you can define the generic type on the method. Class Demo <T> {public void show (T t) {System. out. println ("show:" + t);} public <Q> void print (Q q Q) {System. out. println ("print:" + q);} public static <W> void method (W t) {System. out. println ("method:" + t) ;}} class GenericDemo4 {public static void main (String [] args) {Demo <String> d = new Demo <String> (); d. show ("haha"); d. print (5); d. print ("hehe"); Demo. method ("hahahahha") ;}// the generic type is defined on the interface. Interface Inter <T> {void show (T t);} class InterImpl <T> implements Inter <T> {public void show (T t) {System. out. println ("show:" + t) ;}} class GenericDemo5 {public static void main (String [] args) {InterImpl <Integer> I = new InterImpl <Integer> (); i. show (4 );}}? Wildcard. It can also be understood as a placeholder. Limitations of generics ;? Extends E: it can receive the E type or the sub-type of E. Upper Limit .? Super E: can receive the parent type of E or E. Lower limit Map set: This set stores key-value pairs. One to one. The key must be unique. What is map set used? When this ing relationship exists between data, you must first consider the map set. Common Methods of Map set: 1. Add. Put (K key, V value) // Add an element. If an element is added, the key is the same. Then the added value overwrites the corresponding value of the original key. // The put method returns the overwritten value. PutAll (Map <? Extends K ,? Extends V> m) 2, delete. Clear () remove (Object key) // return the deleted Value 3. ContainsValue (Object value) containsKey (Object key) isEmpty () 4, get. Get (Object key) size () values () entrySet () keySet () Map | -- Hashtable: The underlying layer is the data structure of the hash table, and the null key value cannot be saved. This set is synchronized by threads. Jdk1.0. | -- HashMap: The underlying layer is the data structure of the hash table. The null value and the null key are allowed. This set is not synchronized. Replace hashtable with jdk1.2. | -- TreeMap: The underlying layer is the binary tree data structure. The thread is not synchronized. It can be used to sort keys in the map set. Similar to Set. At the underlying layer of Set, Map sets are used. Two methods to retrieve a map Set: 1. Set <k> keySet: stores all the keys in the map to the Set. Because set has an iterator. All the keys that can be retrieved iteratively are obtained according to the get method. Obtains the value corresponding to each key. Principle of Map Collection removal: converts a map set into a set. In the iterator. Example: // first obtain the Set of all keys in the map Set, keySet (); Set <String> keySet = map. keySet (); // with the Set. You can get its iterator. Iterator <String> it = keySet. iterator (); while (it. hasNext () {String key = it. next (); // with a key, you can use the get method of the map set to obtain the corresponding value. String value = map. get (key); System. out. println ("key:" + key + ", value:" + value) ;}2, Set <Map. entry <k, v> entrySet: stores the ing relationships in the map set into the set, and the Data Type of the ing is Map. the Entry is actually a static internal interface in Map. Why is it defined internally? Only when a Map set and a key-value pair are available can a key-value ing relationship be established. A link is an internal thing in the Map set. In addition, this transaction directly accesses the elements in the Map set. Example: Set <Map. entry <Student, String> entrySet = hm. entrySet (); Iterator <Map. entry <Student, String> iter = entrySet. iterator (); while (iter. hasNext () {Map. entry <Student, String> me = iter. next (); Student stu = me. getKey (); String addr = me. getValue (); System. out. println (stu + "......... "+ addr );} **************************************** * ***************************** Collection, list, Set, and Map usage and difference Java sprin The call relationship of the g MVC framework is clear, but it is still a long way to go. Many things are mysterious to me, such as list, set, map, etc, let's unveil them one by one today. When there are a lot of classes and interfaces encapsulated by systems that need to be remembered, these contents will be coming soon. First, let's take a look at the relationship between them. The Collection of Collection objects of the Collection interface sub-interfaces are saved in an orderly order according to the entry. Repeated │ Implementation of the class linked List insertion and deletion without synchronization thread unsafe │ the ├ ArrayList interface implements Random Access to arrays of classes. No synchronization thread is secure. │ the parallel Vector interface implements class array synchronization thread security. │ the parallel Stack parallel Set subinterface only receives the data once, perform an internal sorting, such as ├ HashSet │ └ LinkedHashSet └ TreeSet. For List, the sequence is concerned. It ensures that the specific sequence of elements is maintained (the same element is allowed ), you can use this interface to precisely control the insert position of each element. You can use indexes (the position of an element in the List, similar to the array subscript) to access the elements in the List. For a Set, you only care about whether an element belongs to the Set (the same element is not allowed), but about its sequence. Set of Map interface key-value pairs; ├ Hashtable interface implementation class synchronous thread security ├ HashMap interface implementation class no synchronization thread unsafe │ ├ LinkedHashMap │ └ WeakHashMap implements TreeMap └ IdentifyHashMap for Map, the biggest feature is key-value ing, which is a one-to-one ing. Keys cannot be repeated and values can be used. Therefore, keys are used to index values. The put (Object key, Object value) method adds a "value" (desired) and a "key" (key) associated with the "value" (use it for search ). The get (Object key) method returns the "value" associated with the given "key ". Map also saves a copy of each element, but this is based on the "key". Map also has built-in sorting, so it does not care about the order of element addition. If the order of adding elements is important to you, use javashashset or javashashmap. for efficiency, because Map uses Hash hash, It is faster to search for elements than ArrayList. A more refined Summary: Collection is a Collection of objects. Collection has two sub-interfaces: List and SetList. You can use subscript (1, 2 ..) to obtain the value. The value can be repeated, while the Set can only be Set through the cursor. The value is an ArrayList that cannot be repeated, and the Vector and sorted List are the implementation class of the List. ArrayList is thread unsafe, the Vector is thread-safe, and the underlying layers of these two classes are implemented by arrays. The worker list is thread-insecure, at the underlying layer, Map implemented by the linked list is the key-value pair set HashTable and HashMap. The implementation class HashTable of Map is thread-safe. It is not thread-safe to store the null value HashMap, null values can be stored ------- <a href = "http://www.itheima.com" target = "blank"> android training </a>, <a h Ref = "http://www.itheima.com" target = "blank"> java training </a>. We look forward to communicating with you! ---------