Java Foundation--java Collection (ii)

Source: Internet
Author: User

Iterator interface

The iterator interface is also a member of the Java Collection framework, but it is not the same as the collection of the collection series, the map series: The collection Series collection, the Map series collection is primarily used to load other objects, The iterator is used primarily to traverse the elements in the collection collection, and the iterator object is also known as an iterator.

The iterator interface hides the underlying details of the various collection implementation classes, providing the application with a unified programming interface that iterates through the collection collection elements. The following three methods are defined in the iterator interface:

Boolean Hasnext (); If the collection element being iterated has not been traversed, return trueobject next ();  Returns the next element in the collection void Remove ();  Delete the previous next method in the collection returns the element

The following program demonstrates traversing a collection element through the iterator interface.

ImportJava.util.*; Public classiteratortest{ Public Static voidMain (string[] args) {//Create a collectionCollection books =NewHashSet (); Books.add ("Lightweight Java EE Enterprise Application Combat"); Books.add ("Crazy Java Handout"); Books.add ("Crazy Android Handout"); //gets the iterator that corresponds to the books collectionIterator it =Books.iterator ();  while(It.hasnext ()) {//the data type returned by the It.next () method is of type object,//forced type conversion requiredString book =(String) it.next ();            SYSTEM.OUT.PRINTLN (book); if(Book.equals ("Crazy Java Handout")))            {                //removes the element returned from the previous next method from the collectionIt.remove (); }            //Assigning a value to a book variable does not alter the collection element itselfBook = "Test string";//①} System.out.println (books); }}

Show Results:

Lightweight Java EE Enterprise app crazy Android handout crazy Java handout [lightweight Java EE Enterprise Application combat, Crazy Android Handout]

Java Foundation--java Collection (ii)

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.