Set Map, variable parameters, Collections, mapcollections
Features of Map sets
Java. util. Map <K, V> interface: set, which is a double-row set
Features of the Map set:
1. It is a double-row set. key and value values must be assigned at the same time when values are assigned.
2. It is an unordered set (the order of elements stored and retrieved may be inconsistent)
3. The key value cannot be repeated, and the value can be repeated.
4. One key can only correspond to one vlaue
5. When defining a set, you can use the same data type as key and value, or use different data types.
The first method for traversing a Map set
The first way to traverse a Map set: To search for values through Jian
There is a method in the Map set: keySet
Set <K> keySet () returns the Set view of the keys contained in the ing. Store the keys in the Map Set to a Set.
Steps for traversing a Map set:
1. Define a Map set and add elements to the set
2. Call the keySet method in the Map Set to store the keys in the Map Set to a Set.
3. traverse the Set to obtain all the keys of the Map Set.
4. Use the Map set method to get the obtained values through the obtained keys.
1 public static void main (String [] args) {2 // 1. define a Map set and add elements 3 Map <String, String> map = new HashMap <String, String> (); 4 map to the set. put ("a", "1"); 5 map. put ("B", "2"); 6 map. put ("c", "3"); 7 map. put ("d", "4"); 8 // 2. call the keySet method in the Map Set and store the keys in the Map Set to the 9 set <String> Set = map in the Set set Set. keySet (); 10 // System. out. println (set. getClass (); 11 // 3. traverse the Set set to obtain all keys of the Map Set. // use the Iterator to traverse 13 Iterator <String> it = set. iterator (); 14 while (it. hasNext () {15 String key = it. next (); 16 // 4. after obtaining the key, use the Map set method to get and find the value 17 String value = map. get (key); 18 System. out. println (key + "... "+ value); 19} 20 System. out. println ("----------------"); 21 // use enhanced for traversal 22 for (String key: set) {23 // 4. after obtaining the key, use the Map set method to get and find the value 24 String value = map. get (key); 25 System. out. println (key + "... "+ value); 26} 27 System. out. println ("----------------"); 28 // use enhanced for traversal 29 for (String key: map. keySet () {30 // 4. after obtaining the key, use the Map set method to get and find the value 31 String value = map. get (key); 32 System. out. println (key + "... "+ value); 33} 34}
The second method of Map Collection Traversal
The second way to traverse a Map set: the Way to Traverse Key-value pairs
There is a method in the Map set: entrySet
Set <Map. Entry <K, V> entrySet () returns the Set view of the ing in this ing.
Traversal steps:
1. Define a Map set and add elements to the set
2. Call the method entrySet in the Map Set to put every ing (marriage certificate) in the Map Set into the Set.
3. traverse the Set to obtain each Entry ing Entry <K, V>
4. Use the getKey and getValue methods in Entry <K, V> to obtain the health and value.
1 public static void main (String [] args) {2 // 1. define a Map set and add elements 3 Map <String, String> map = new HashMap <String, String> (); 4 map to the set. put ("a", "1"); 5 map. put ("B", "2"); 6 map. put ("c", "3"); 7 map. put ("d", "4"); 8/* 9*2. call the method entrySet in the Map Set to put every ing (marriage certificate) in the Map Set into the Set. The access method of the 10 * member internal class: External class. internal class (Map. entry) 11 */12 Set <Map. entry <String, String> set = map. entrySet (); 13 // 3. traverse the Set to obtain each Entry ing Entry <K, V> 14 // use the Iterator to traverse the Set 15 Iterator <Map. entry <String, String> it = set. iterator (); 16 while (it. hasNext () {17 Map. entry <String, String> entry = it. next (); 18 // 4. use the getKey and getValue methods in Entry <K, V> to obtain the health key and value 19 String key = entry. getKey (); 20 String value = entry. getValue (); 21 System. out. println (key + "... "+ value); 22} 23 System. out. println ("---------------------"); 24 // use the enhanced for traversal Set 25 for (Map. entry <String, String> entry: set) {26 // 4. use the getKey and getValue methods in Entry <K, V> to obtain the health and value 27 String key = entry. getKey (); 28 String value = entry. getValue (); 29 System. out. println (key + "... "+ value); 30} 31 System. out. println ("---------------------"); 32 // use the enhanced for traversal Set 33 for (Map. entry <String, String> entry: map. entrySet () {34 // 4. use the getKey and getValue methods in Entry <K, V> to obtain the health and value 35 String key = entry. getKey (); 36 String value = entry. getValue (); 37 System. out. println (key + "... "+ value); 38} 39}
HashMap stores custom type key values
HashMap stores custom type key values
The custom type is used as the value of the Map set.
The custom type is used as the key of the Map set.
Remember: When to override hashCode and equals for custom types
1. Use HashSet to store custom types
2. Use the HashMap set and use the custom type
Hashtable
Map implementation class Hashtable
The underlying data structure is a hash table with the same features as hashMap.
Hashtable is a set of thread security and is slow to run.
HashMap is a collection of unsafe threads and runs fast.
The fate of Hashtable is the same as that of Vector. It has been replaced by a more advanced HashMap since JDK1.2.
HashMap allows null values to be stored.
Hashtable cannot store null values.
Hashtable his child, sub-class Properties is still active on the development stage
LinkedHashMap set features
Java. util. LinkedHashMap extends HashMap implements Map
LinkedHashMap set features:
1. Hash Table + linked list: two-way linked list, which can ensure the iteration order
2. Duplicate keys are not allowed.
Collections
Java. util. Collections: tool class used to operate Collection Sets
The methods in the tool class are static methods, which can be directly used by class names.
Public static <T> void sort (List <T> list) // sorts set Elements
Public static void shuffle (List <?> List) // collection element storage location disruption
Variable parameters
New features after JDK1.5
Prerequisites: the data type of the method parameter is determined, but the number of parameters is not sure.
Format:
Modifier return value type method name (data type... variable name ){
}
... Indicates that the method can receive multiple parameters of the same data type.
The underlying layer of variable parameters can be viewed as an array.
Considerations for variable parameters:
1. Only one variable parameter can be used for a method parameter.
2. If there are multiple method parameters, variable parameters must be written at the end of the parameter list
1 public static int add (int... arr) {2 System. out. println (arr); // [I @ 104c575 3 System. out. println (arr. length); 4 int sum = 0; 5 // traverse variable parameters --> traverse array 6 for (int I: arr) {7 sum + = I; 8} 9 10 return sum; 11}
Static Import
JDK1.5 new feature, static Import
Reduce the amount of code developed
Standard writing, which can be used only when the package is imported
Import static java. lang. System. out; the end must be a static member.
Package cn. itcast. demo05;
Import java. util. ArrayList;
Import java. util. Collections;
Import java. util. HashMap;
1/* 2 * case of landlords with sorting function: 3*1. prepare the Card 4*2. shuffling 5*3. licensing 6*4. sort 7*5. card viewing 8 */9 public class DouDiZhu {10 public static void main (String [] args) {11 // 1. prepare the card 12 // create a Map set of storage sequence numbers and face values 13 HashMap <Integer, String> poker = new HashMap <Integer, String> (); 14 // create a List set of storage serial numbers 15 ArrayList <Integer> pokerNumber = new ArrayList <Integer> (); 16 // create an array of serial numbers 17 String [] numbers = {"2", "A", "K", "Q", "J", "10 ", "9", "8", "7", "6 ", "5", "4", "3"}; 18 // create a color array 19 String [] colors = {"? ","? ","? ","? "}; 20 // first store the king and the king in the collection, 21 int index = 0; 22 poker. put (index, ""); 23 pokerNumber. add (index); 24 index ++; 25 poker. put (index, "John"); 26 pokerNumber. add (index); 27 index ++; 28 29 // uses loop nesting to traverse two arrays, forming 52 cards 30 for (String number: numbers) {31 for (String color: colors) {32 // Add the pack card to 33 poker in the set. put (index, color + number); 34 pokerNumber. add (index); 35 index ++; 36} 37} 38 // System. out. println (poker); 39 // System. out. println (pokerNumber); 40 41 // 2. shuffling: The card number is 42. // use the shuffle 43 Collections method in Collections. shuffle (pokerNumber); 44 // System. out. println (pokerNumber); 45 46/* 47*3. licensing: The number of cards issued is 48 *. define 4 sets to store 3 players and 1 base card 49 * B. traverse the List set of the storage serial number 50 * c. use the index % of the list set for licensing 51 * Note: first judge the base card 52 */53 //. define 4 sets to store 3 players and 1 base card 54 ArrayList <Integer> player01 = new ArrayList <Integer> (); 55 ArrayList <Integer> player02 = new ArrayList <Integer> (); 56 ArrayList <Integer> player03 = new ArrayList <Integer> (); 57 ArrayList <Integer> diPai = new ArrayList <Integer> (); 58 59 // B. traverse the List set of the storage serial number 60 for (int I = 0; I <pokerNumber. size (); I ++) {61 // defines the variable. The serial number of the receiving row is 62 int number = pokerNumber. get (I); 63 // c. use the index % of the list set for licensing 64 if (I> = 51) {65 // storage card 66 diPai. add (number); 67} else if (I % 3 = 0) {68 // authorize Player 1 69 player01.add (number ); 70} else if (I % 3 = 1) {71 // issue PLAYER 2 72 player02.add (number); 73} else if (I % 3 = 2) {74 // issue Player 3 75 player03.add (number); 76} 77} 78/* System. out. println (player01); 79 System. out. println (player02); 80 System. out. println (player03); 81 System. out. println (diPai); */82 83 // 4. sort 84 // use the method sort 85 Collections in Collections. sort (player01); 86 Collections. sort (player02); 87 Collections. sort (player03); 88 Collections. sort (diPai); 89 90/* System. out. println (player01); 91 System. out. println (player02); 92 System. out. println (player03); 93 System. out. println (diPai); */94 95/* 96*5. card viewing 97 */98 // call card viewing method 99 lookPoker ("Liu Dehua", player01, poker); 100 lookPoker ("Zhou runfa", player02, poker ); 101 lookPoker ("Zhou xingchi", player03, poker); 102 lookPoker ("card", diPai, poker ); 103} 104 105/* 106 * define a card watching method 107 * return value type: void108 * method name: lookPoker109 * parameter list: Set of players and cards, the Map set of the storage row is 110 *. Use the lookup table method to view the card: 111 * traverses the List set and obtains the key of the Map set, use key to search for value112 */113 public static void lookPoker (String name, ArrayList <Integer> list, HashMap <Integer, String> Map) {114 System in the map set. out. print (name + ":"); 115 // traverse the List set to obtain the Map set key116 for (Integer key: list) {117 // use the key to search for value118 String value = Map in the map set. get (key); 119 System. out. print (value + ""); 120} 121 System. out. println (); // wrap 122} 123}