Iterator collection, iterator

Source: Internet
Author: User

Iterator collection, iterator

Package com. starain. Iterator;
/* The shortcut for code sorting is Ctrl + Shift + F.
* Enter the shortcut key main character + Alt +/in the main method/
* Output shortcut key: sysout character + Alt + /*/

Import java. util. *; // two commonly used packages, java. lang Language Pack and java. util practical package
/* Java. lang is automatically introduced,
* Contains data packages (Byte, Short, Integer, Long, Float, Double, Character, Boolean ),
* String class (constant String, variable StringBuffer, StringBuilder)
* Math for mathematics,
* System and Runtime class (System, Runtime ),
* Class operation Class (Class, ClassLoader)
* Manual introduction of java. util
* Date, Calendar, GregorianCalendar)
* GregorianCalendar cal = (GregorianCalendar) GregorianCalendar. getInstance ();
* Cal. isLeapYear (year); returns the boolean Type and determines whether it is a leap year.
* Collection class (Collection -- Set -- HashSet, TreeSet;
* Collection -- List -- ArrayList, Vector, sorted List;
* Map -- HashTable, HashMap, WeakHashMap)
**/

Public class IteratorDemo1 {
Public static void main (String [] args ){
/* List is an ordered set in the original order, which can be repeated.
* ArrayList asynchronous mode, high performance, but thread security
* Vector is a synchronization mode with low performance, but thread security */
List <String> list1 = new ArrayList <String> (); // List is an interface and cannot be directly instantiated. The generic type and subclass instantiation method are used.

/* Add operation */
List1.add ("my ");
List1.add ("name ");
List1.add ("is ");
List1.add ("starain ");

/* Determine whether the operation is null */
Boolean bool1 = list1.isEmpty ();

If (! Bool1 ){

/* Output as an array all day */
System. out. println (list1 );

/* Enhanced for loop printing mode */
For (String str: list1 ){
System. out. print (str + "");
}

System. out. println ("\ n =================== ");
System. out. print ("two gorgeous split lines ");
System. out. println ("\ n =================== ");

/* Iterator interface Printing Output Mode
* The standard form of set output is the Iterator interface.
* Principle: Iterator is a specialized iterative output interface. It identifies the elements one by one to determine whether there is any content. If there is any content, it extracts the content */
Iterator <String> iterator1 = list1.iterator ();
While (iterator1.hasNext ()){
System. out. print (iterator1.next () + "");
}

/* Remove operation */
List1.remove ("is ");

System. out. println ();
For (String str2: list1 ){
System. out. print (str2 + "");
}
}

System. out. println ("\ n =================== ");
System. out. print ("two gorgeous split lines ");
System. out. println ("\ n =================== ");

/* Set is a Set that does not repeat, and does not allow repetition. If it is repeated, It is overwritten before
* HashSet unordered (non-original)
* Automatic TreeSet sorting. Based on Map, an error is returned when the sorting cannot be completed */
Set <String> set1 = new HashSet <String> ();
Set <String> set2 = new TreeSet <String> ();

/* Add an element to HashSet set1 */
Set1.add ("A"); // copy and paste the shortcut key Ctrl + Alt + downward key
Set1.add ("B ");
Set1.add ("B ");
Set1.add ("D ");
Set1.add ("C ");
Set1.add ("E ");
Set1.add ("F ");
Set1.add ("C ");
Set1.add ("G ");

/* Output */
System. out. println (set1 );

/* Add elements to TreeSet set2 */
Set2.add ("A"); // copy and paste the shortcut key Ctrl + Alt + downward key
Set2.add ("B ");
Set2.add ("B ");
Set2.add ("D ");
Set2.add ("C ");
Set2.add ("E ");
Set2.add ("F ");
Set2.add ("C ");
Set2.add ("G ");

/* Output */
System. out. println (set2 );


/* Map example
* The storage format of the Map interface is key-> value. For example, the address book uses the name key to locate the corresponding phone number value.
* Map <K, V> two generics, corresponding to key and value */
Map <String, String> map1 = new HashMap <String, String> ();

/* Map is added as the put () method */
Map1.put ("key1", "www ");
Map1.put ("key2 ",".");
Map1.put ("key3", "starain ");
Map1.put ("key4 ",".");
Map1.put ("key5", "index ");
Map1.put ("key6 ",".");
Map1.put ("key7", "com ");

/* Determine the memory first
* Determine whether the value is containKey (key) by using the key)
* Use value to determine containValue (value )*/
Boolean boolkey1 = map1.containsKey ("key1 ");
Boolean boolkey2 = map1.containsKey ("key99 ");
If (boolkey1 ){

/* Print value through key */
String str1 = map1.get ("key1 ");
System. out. println (str1 );
} Else System. out. println ("key1 does not exist ");
If (boolkey2 ){
String str99 = map1.get ("key99 ");
System. out. println (str99 );
} Else System. out. println ("key99 does not exist ");


/* Iterator interface printing */

/* Print the key */
Set <String> s = map1.keySet (); // the return value of keySet () is Set.
Iterator <String> i1 = s. iterator ();
While (i1.hasNext ()){
System. out. print (i1.next () + ""); // The remove operation on the collection is prohibited in the action.
}

System. out. println ("\ n =================== ");
System. out. print ("two gorgeous split lines ");
System. out. println ("\ n =================== ");

/* Print value */
Collection <String> c = map1.values (); // value () returns the Collection type.
Iterator <String> i2 = c. iterator ();
While (i2.hasNext ()){
System. out. print (i2.next () + "");
}
}
}

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.