A summary of the Java list Loop and the map loop _java

Source: Internet
Author: User

Summary of Java list loop and map loop

Do a list and map of the summary, there is no technical content, all the review of the API.

The test environment is under JUNIT4, and it is the same without writing a main method yourself.

First, there are three loops of the list:



   @Test public 
void Forlisttest () { 
  list<string> List = new arraylist<string> (); 
  List.add ("1"); 
  List.add ("2"); 
  List.add ("3"); 
  List.add ("4"); 
  List.add ("5"); 
 
  Iterator loops do not need to know the size and type of the collection, the best choice for 
  (@SuppressWarnings ("Rawtypes") 
  iterator iterator = List.iterator (); Iterator.hasnext ();) { 
    String list = (string) iterator.next (); 
    System.out.println (") iterator for:===============" + list); 
  } 
 
  foreach is not required to know the length of the collection for 
  (String list:list) { 
    System.out.println ("foreach:=================") relative to the for. + list); 
  } 
 
  A For loop needs to know the size of the collection and requires an ordered for 
  (int i = 0; i < list.size (); i++) { 
    System.out.println () for============= ===== "+ list.get (i)); 
  } 
} 

Then there are four loops of map:


   @Test public void Formaptest () {map<string, string> Map = new hashmap<string, string> (); 
  Map.put ("01", "1"); 
  Map.put ("02", "2"); 
  Map.put ("03", "3"); 
  Map.put ("04", "4"); 
  Map.put ("05", "5"); 
  Set<string> keyset = Map.keyset (); 
        1.keyset foreach method for (String key:keyset) {System.out.println ("1) Keyset:" + "key:" + key + "value:" 
  + map.get (key)); 
  } set<entry<string, string>> entryset = Map.entryset ();  
    2.entryset iterative method for (@SuppressWarnings ("Rawtypes") Iterator iterator = Entryset.iterator (); Iterator.hasnext ();) { 
        @SuppressWarnings ("Unchecked") entry<string, string> Entry = (entry<string, string>) iterator 
    . Next (); 
  System.out.println (") Entryset,iterator:key:" + entry.getkey () + "value:" + entry.getvalue ()); //3. Recommended, capacity Max for (entry<string, string> entry:entryset) {System.out.println (") Entryset,foreach:ke Y: "+ entry.geTKey () + "value:" + entry.getvalue ()); 
  } collection<string> values = Map.values (); 
  4. The method for only looping out the value for (String value:values) {System.out.println (") Values,just for Values,value:" + value); 
 } 
 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.