Iterator Class

Iterator Class with iterator() Method

In this blog, We will see how to fetch data from ArrayList Class using Iterator class using iterator() method. We will also see how to use hasNext() method for check data and next() method for store data in String class.

In this blog, we will see display 5 country name in uppercase using ArrayList, Iterator, iterator() method, hasNext() method and next() method. We have to convert next() method data into String wrapper class using (String) casting.

 

Let’s see Example on Iterator Class

 

import java.util.*;

class collection4

{

                public static void main(String args[])

                {

                                ArrayList<String> list1 = new ArrayList<String>();

 

                                list1.add(“India”);

                                list1.add(“USA”);

                                list1.add(“Australia”);

                                list1.add(“Canada”);

                                list1.add(“Sri-Lanka”);

 

                                Iterator itr = list1.iterator();

                                while(itr.hasNext())                                       

{

                                                String country = (String)itr.next();           

System.out.println(country.toUpperCase());     

                                }

                }

}

 

Output

INDIA

USA

AUSTRALIA

CANADA

SRILANKA

 

Video on simple Iterator Class Example

 

 

For more videos on Java visit our YouTube Channel

 

Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

854total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint