The Hasnext method and next method of Iterator

Source: Internet
Author: User

Both methods have a pointer to the move, and the difference is that one returns a Boolean and a return object:

Hasnext (): Determines whether the current element exists and does not point to the move

Next (): Returns the current element and refers to the next element

Take a look at the code:

linkedhashmap<string, string> myMap = new linkedhashmap<string, string> ();

MyMap. Put ("0", "0.0");

MyMap. Put ("1", "1.0");

MyMap. Put ("2", "2.0");

Set<string> MySet = Mymap.keyset ();

int i=0,j=0;

for (iterator<string> Iterator = Myset.iterator (); Iterator.hasnext ();) {// last i=3

Iterator.hasnext ();//The execution of this method here does not affect the overall traversal

i++;

String Theone=iterator.next ();

System.out.println (i+ "--" +theone);

}

for (iterator<string> Iterator = Myset.iterator (); Iterator.hasnext ();) {// last j=2

j + +;

String Theone=iterator.next ();

System.out.println (j+ "--" +theone);

Iterator.next ();//The execution of this method here affects the execution of the overall traversal

}

The Hasnext method and next method of Iterator

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.