Wu Yuxiong - natural born JAVA development of learning: Collections Framework

Import Classes in java.util *. ; 
 
public  class the Test {
  public  static  void main (String [] args) { 
     List <String> List = new new the ArrayList <String> (); 
     List.add ( "the Hello" ); 
     List.add ( " world " ); 
     List.add ( " hahahaha " );
      // first traversal method using foreach traverse List 
     for (STR String: List) {             // can be rewritten for (int i = 0; i <list.size ( ); i ++) this form 
        System.out.println (STR); 
     } 
 
     // second traverse, the array becomes a list of content related to traverse 
     String [] strArray =new new String [list.size ()]; 
     List.toArray (strArray); 
     for ( int I = 0; I <strArray.length; I ++) // herein may be rewritten as foreach (String str: strArray) This form 
     { 
        System.out.println (strArray [I]); 
     } 
     
    // third loop uses the iterator associated to traverse 
     
     the iterator <String> = ITE list.iterator ();
      the while (ite.hasNext ()) // next determines a after the element has a value of 
     { 
         System.out.println (ite.next ()); 
     } 
 } 
}
import java.util.*;
 
public class Test{
     public static void main(String[] args) {
      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //The second 
      System.out.println ( "traversal key and value by using Map.entrySet Iterator:" ); 
      the Iterator <of Map.Entry <String, String >> = IT . EnumMap.entrySet () Iterator ();
       the while (IT .hasNext ()) { 
       of Map.Entry <String, String> = entry it.next (); 
       System.out.println ( "Key =" entry.getKey + () + "and value =" + entry.getValue () ); 
      } 
      
      // third: recommended, especially when a large-capacity 
      System.out.println ( "key and traversing through Map.entrySet value" );
       for (of Map.Entry <String, String> entry: EnumMap.entrySet ( )) { 
       System.out.println ( "Key =" + entry.getKey() + " and value= " +entry.getValue ()); 
      } 
    
      // fourth 
      System.out.println ( "through Map.values () through all value, but can not traverse the Key" );
       for (String V: map.values ()) { 
       System.out.println ( "value =" + V); 
      } 
     } 
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/10967050.html