Java code snippets _ 5

Source: Internet
Author: User

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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.