) Iterator interface of the Collection class

Source: Internet
Author: User

Iterator Interface

This is also a very useful interface. In practice, iterator will be used for classes that use the collection interface.
However, all the container classes that implement the collection interface have an iterator () method to return an iterator Interface
. It is used to traverse the collection elements (that is, to retrieve the elements in the collection as you want ).

These three methods are defined in this interface.
Boolean hasnext (); // determines whether there are any elements on the right of the cursor.
Object next (); // return the elements specified in the cursor above, and then move the cursor to the next position. Of course, the retrieved items must be converted to appropriate types.
Void remove (); // Delete the elements on the left of the cursor. This operation can only be performed once after the next operation is completed.

Here is a small example:
Note the place where 1 is written
Example 1
Source code:

Import Java. util. *;
public class testiterator
{< br> Public static void main (string [] ARGs)
{< br> // collection C = new sorted list (); // 4
collection C = new hashset (); // 5
C. add (new user ("first", "FN");
C. add (new user ("second", "Sn");
C. add (new user ("third", "tn");
C. add (new user ("four", "fourn");
C. add (new user ("four", "fourn"); // note that this line is the same as 1 in the previous line

Iterator it = C. iterator (); // 6
While (it. hasnext ())
{
// Here we need to forcibly convert the corresponding type because next () returns the object type
User user = (User) it. Next ();
System. Out. println ("firstname:" + User. firstname + "lastname:" + User. lastname );
} // 7
}
}
Class user
{
Public String firstname;
Public String lastname;

Public user (string firstname, string lastname)
{
This. firstname = firstname;
This. lastname = lastname;
}
Public String tostring ()
{
Return firstname + "" + lastname;
}
}

Result:
Firstname: Third lastname: tn
Firstname: Second lastname: Sn
Firstname: Four lastname: fourn // 2
Firstname: First lastname: FN
Firstname: Four lastname: fourn // 3

Check the result. It's a little different from what we think. Remember that we mentioned earlier that there is no sequence of set. And does not repeat
So why do we repeat the places 2 and 3 here because the referenced addresses are different?
The two objects do not have equals. Now you should understand what it means. You can remove the 4 annotation and then annotate it on 5,
You will find that their results are ordered, but the important part of this example is the Part 6-7.Code, We are mainly
I'm talking about iterator. You should know how it works when you see the result.

Example 2.
In this example, the remove () method in iterator is used to remove some of its elements. If the length of firstname is less than 6 bits
Let's look at the example first. Otherwise, the masked. User class will not be written any more than above.

Import java. util .*;
Public class testiterator
{
Public static void main (string [] ARGs)
{
Collection c = new vertex list ();
// Collection C = new hashset ();
C. Add (new user ("first", "FN "));
C. Add (new user ("second", "Sn "));
C. Add (new user ("third", "tn "));
C. Add (new user ("four", "fourn "));
C. Add (new user ("four", "fourn"); // note that this line is the same as the previous line.

For (iterator it = C. iterator (); it. hasnext ();)
{
User user = (User) it. Next ();
If (user. firstname. Length () <6)
{
It. Remove ();
// C. Remove (User); // Note 1
}
}
System. Out. println (C );
}
}

Result:
[Second Sn]

Note that NOTE 1 where the iterator cannot use the remove () method in the collection because the iterator is
The automatic lock function is implemented, so it cannot be used. If it is used, no error will be reported during compilation, but it will be thrown during execution.
Exception. You can try it.

Example 3.
Here we will briefly discuss the enhanced for loop.
Let's take a look at the examples and results first.

The source code is as follows:

Import java. util .*;
Public class testenhancedfor
{
Public static void main (string [] ARGs)
{
Int [] arr = {1, 2, 3, 4 };
For (int I: ARR)
{
System. Out. println (I );
}
Collection c = new arraylist ();

C. Add (new string ("aaaa "));
C. Add (new string ("BBBB "));
C. Add (new string ("CCCC "));

For (Object O: C)
{
System. Out. println (O );
}

}
}
Result:
1
2
3
4
Aaaa
Bbbb
CCCC

enhancement of the For Loop Defect
for Arrays-it is not convenient to access the lower tag value
for collections ------ compared with iterator, the content in the set cannot be conveniently deleted. In fact, the iterator is also called internally.
conclusion: In addition to simple traversal and reading of the content, those with development experience do not recommend using the enhanced for loop

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.