Package javaee. china. cxp;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. LinkedHashMap;
Import java. util. Map;
Import java. util. Set;
Import java. util. Map. Entry;
Import org. junit. Test;
/**
* Enhance the for Loop
* Jdk1.5 and above are supported.
* Adding a for loop can only be used in arrays or collections that implement the Iterable interface.
* Adding for is only suitable for retrieving data.
*/
Public class Demo_5_for {
@ Test
Public void demo5_1 (){
/**
* Enhance the for loop array Experiment
*/
Int [] arr = {1, 2, 3, 4, 5, 6, 7 };
For (int I: arr ){
System. out. println (I );
}
}
/**
* Enhance the for loop array assignment Experiment
* It proves that the enhancement for loop cannot be assigned a value and is only suitable for retrieving data.
* If you want to assign values, use the traditional For loop.
*/
@ Test
Public void demo5_1_1 (){
Int [] arr = {1, 2, 3, 4, 5, 6, 7 };
For (int I: arr ){
Arr [I] = 10; // array cannot be fully awake when I is an object
}
For (int I: arr ){
System. out. println (I );
}
}
/**
* Demonstrate the traditional access method 1 of HashMap
*/
@ Test
Public void demo5_2 (){
Map map = new HashMap ();
Map. put ("1", "");
Map. put ("2", "B ");
Map. put ("3", "c ");
Map. put ("4", "d ");
Set set = map. keySet ();
Iterator it = set. iterator ();
While (it. hasNext ()){
String key = (String) it. next ();
String value = (String) map. get (key );
System. out. println (key + ":" + value );
}
}
/**
* LinkedHashMap linear storage experiment
* I personally think that LinkedHashMap is easier to use than HashMap because the storage order is the same.
*/
@ Test
Public void demo5_3 (){
Map map = new LinkedHashMap (); // LinkedHashMap belongs to the same linear list access sequence
Map. put ("1", "");
Map. put ("2", "B ");
Map. put ("3", "c ");
Map. put ("4", "d ");
Set set = map. keySet ();
Iterator it = set. iterator ();
While (it. hasNext ()){
String key = (String) it. next ();
String value = (String) map. get (key );
System. out. println (key + ":" + value );
}
}
/**
* Demonstrate the traditional access method 2 of HashMap
*/
@ Test
Public void demo5_4 (){
Map map = new HashMap ();
Map. put ("1", "");
Map. put ("2", "B ");
Map. put ("3", "c ");
Map. put ("4", "d ");
Set set = map. entrySet ();
Iterator it = set. iterator ();
While (it. hasNext ()){
Map. Entry entry = (Entry) it. next ();
String key = (String) entry. getKey ();
String value = (String) entry. getValue ();
System. out. println (key + ":" + value );
}
}
/**
* Enhance the for loop HashMap Experiment
* HashMap does not implement the Iterable interface to implement the Iterable interface in disguise.
* Demonstrate the traditional access method 1 of HashMap
*/
@ Test
Public void demo5_5 (){
Map map = new HashMap ();
Map. put ("1", "");
Map. put ("2", "B ");
Map. put ("3", "c ");
Map. put ("4", "d ");
Set set = map. keySet ();
For (Object j: set ){
String key = (String) j;
String value = (String) map. get (key );
System. out. println (key + ":" + value );
}
/* While (it. hasNext ()){
String key = (String) it. next ();
String value = (String) map. get (key );
System. out. println (key + ":" + value );
}*/
}
/**
* Enhance the for loop HashMap Experiment
* HashMap does not implement the Iterable interface to implement the Iterable interface in disguise.
* Demonstrate the traditional access method 2 of HashMap
*/
@ Test
Public void demo5_6 (){
Map map = new HashMap ();
Map. put ("1", "");
Map. put ("2", "B ");
Map. put ("3", "c ");
Map. put ("4", "d ");
Set set = map. entrySet ();
For (Object j: set ){
Entry entry = (Entry) j;
String key = (String) entry. getKey ();
String value = (String) entry. getValue ();
System. out. println (key + ":" + value );
}
}
}