How to use Iterator in Java

By: Fazal Emailed: 1789 times Printed: 2623 times    

Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these steps:

  1. Obtain an iterator to the start of the collection by calling the collection's iterator( )
  2. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true.
  3. Within the loop, obtain each element by calling next( ).

For collections that implement List, you can also obtain an iterator by calling ListIterator. As explained, a list iterator gives you the ability to access the collection in either the forward or backward direction and lets you modify an element. Otherwise, ListIterator is used just like Iterator.

Here is an example that implements these steps, demonstrating both Iterator and ListIterator. It uses an ArrayList object, but the general principles apply to any type of collection. Of course, ListIterator is available only to those collections that implement the List interface.

// Demonstrate iterators. 
import java.util.*; 
class IteratorDemo { 
public static void main(String args[]) { 
// create an array list 
ArrayList al = new ArrayList(); 
// add elements to the array list 
al.add("C"); 
al.add("A"); 
al.add("E"); 
al.add("B"); 
al.add("D"); 
al.add("F"); 
// use iterator to display contents of al 
System.out.print("Original contents of al: "); 
Iterator itr = al.iterator(); 
while(itr.hasNext()) {

    Object element = itr.next(); 
    System.out.print(element + " ");


System.out.println(); 
// modify objects being iterated 
ListIterator litr = al.listIterator(); 
while(litr.hasNext()) {

    Object element = litr.next(); 
    litr.set(element + "+");


System.out.print("Modified contents of al: "); 
itr = al.iterator();
while(itr.hasNext()) {

    Object element = itr.next(); 
    System.out.print(element + " ");


System.out.println(); 
// now, display the list backwards 
System.out.print("Modified list backwards: "); 
while(litr.hasPrevious()) {

    Object element = litr.previous(); 
    System.out.print(element + " ");


System.out.println(); 

}

The output is shown here: 

Original contents of al: C A E B D F 
Modified contents of al: C+ A+ E+ B+ D+ F+ 
Modified list backwards: F+ D+ B+ E+ A+ C+

Pay special attention to how the list is displayed in reverse. After the list is modified, litr points to the end of the list. (Remember, litr.hasNext( ) returns false when the end of the list has been reached.) To traverse the list in reverse, the program continues to use litr, but this time it checks to see whether it

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. very good for programing
View Tutorial          By: kiran at 2007-12-06 00:39:47

2. good
View Tutorial          By: sasikiran at 2008-03-30 23:33:24

3. good quick explanation. Thanks
View Tutorial          By: kay at 2008-12-08 21:10:13

4. Timely info right when I needed it. Thanks.
View Tutorial          By: Electric Vehicles at 2008-12-10 15:15:25

5. Good explanation about Iterator. But Object declar
View Tutorial          By: Prasad at 2008-12-22 23:20:59

6. Thankyou it is Working Fine .....
View Tutorial          By: PDCOE at 2009-02-11 23:02:29

7. Great Work for JAVA Beginners
View Tutorial          By: Sudhir Rajgure at 2009-03-01 02:40:00

8. good understandability code
View Tutorial          By: Raja Mahendra Kumar at 2009-03-05 03:11:26

9. Thank for your clear explanation
View Tutorial          By: htnga at 2009-04-05 18:43:25

10. u doing good
View Tutorial          By: matsiko at 2009-04-14 08:05:00

11. tnx .
View Tutorial          By: smokes at 2009-04-15 07:30:19

12. just what i needed to understand iterators, gj
View Tutorial          By: oak at 2009-04-20 15:46:39

13. thats good
View Tutorial          By: P.Kumar at 2009-08-02 10:48:48

14. Thanks, much appreciated!
Saved a great dea

View Tutorial          By: alan at 2009-08-14 01:35:01

15. I'm a beginner, and I was told it is a good practi
View Tutorial          By: Nelson at 2009-08-17 09:13:28

16. tnx, nice code...
View Tutorial          By: Hansoc at 2009-08-18 00:17:58

17. Sorry:

#!/usr/bin/env ruby
a

View Tutorial          By: puma at 2009-09-23 16:39:57

18. Nice!
View Tutorial          By: xeon at 2009-09-30 01:24:05

19. could u pls tell me whcih class is implemted by it
View Tutorial          By: AMIT at 2009-10-04 00:21:30

20. The iterator returned by iterator() does not point
View Tutorial          By: Jeffrey at 2009-10-06 21:45:41

21. sir ji,
i want do learn for loop in java an

View Tutorial          By: Prabhakar at 2009-11-06 00:48:53

22. i know this example this is in mg.hill but i want
View Tutorial          By: srikanta at 2009-11-18 00:06:57

23. thks for help...
View Tutorial          By: Peps at 2010-01-05 02:28:52

24. thks for help...
View Tutorial          By: Peps at 2010-01-05 02:34:15

25. its realy gud which clears minor douts regarding t
View Tutorial          By: Shripad at 2010-01-19 05:40:41

26. good one
View Tutorial          By: kumar saurabh at 2010-06-20 13:29:50

27. it's a usefule example for who's learning java
View Tutorial          By: mathhoang at 2010-07-14 08:29:43

28. Good information...it help me to do mi projecto! t
View Tutorial          By: gnk at 2010-08-12 01:01:50

29. Really this code helped with some idea...
View Tutorial          By: Priya at 2010-09-29 22:21:46

30. PROGRAMMING SUPERB
View Tutorial          By: vinay sharma at 2010-10-01 05:16:23

31. good, it clear the confusion......
nice cod

View Tutorial          By: shree at 2010-10-15 00:57:42

32. thanks a lot
View Tutorial          By: vipul at 2010-10-18 10:23:56

33. Thanks for giving iterator example
View Tutorial          By: Rajesh Kumar at 2010-11-03 05:17:05

34. superb...thanks :-)
View Tutorial          By: sunny at 2010-11-10 07:20:25

35. Thanks for this article. Now I can understand Iter
View Tutorial          By: Rajesh Kumar at 2010-12-14 03:27:25

36. Is there any difference between Iterator and ListI
View Tutorial          By: Ram at 2011-01-02 21:58:48

37. Thanks a lot!! Just looking for such example
View Tutorial          By: Manuel at 2011-01-20 08:17:00

38. just want to ask..how can i use iterator to compar
View Tutorial          By: ikhwan at 2011-02-02 17:14:07

39. well example, but i have to send the received list
View Tutorial          By: amit kumar at 2011-02-21 00:43:15

40. Very Good
View Tutorial          By: Lalit at 2011-02-23 12:24:13

41. its really good.
View Tutorial          By: raj at 2011-03-08 21:13:10

42. Great stuff, learned more from this example than r
View Tutorial          By: David Bandelin at 2011-03-22 07:10:38

43. I am still going through the tutorial, but it seem
View Tutorial          By: shadowwraith at 2011-06-08 08:00:57

44. extremely good
View Tutorial          By: rahul at 2011-06-09 05:59:43

45. good and nice
View Tutorial          By: Elayaraja j at 2011-06-18 05:30:07

46. Nice Example.Good work.keep it up.
Thanks,<

View Tutorial          By: Lucky at 2011-07-08 10:36:38

47. Also note that for reverse ordering, If you have a
View Tutorial          By: Dwight at 2011-07-13 22:13:45

48. This is Nice start for programmer '
Thanks

View Tutorial          By: Sagar Ikhankar at 2011-07-22 06:12:44

49. good explanation for the java beginners...could yo
View Tutorial          By: karthik at 2011-08-19 11:17:53

50. The sample code was all I needed to figure out how
View Tutorial          By: David at 2011-08-28 07:34:28

51. Really Clear
View Tutorial          By: Yasar Arafath at 2011-09-08 07:38:42

52. good job man, its all i needed to understand itera
View Tutorial          By: m3mph1s at 2011-10-11 05:01:17

53. good explanation ,it is very very understandable
View Tutorial          By: chaitanya at 2011-10-27 16:21:55

54. Thanks, short and sweet answer!
View Tutorial          By: A_College_Student at 2011-11-16 03:45:20

55. Thanks dude....
Good work...

View Tutorial          By: ram at 2011-11-18 12:05:31

56. Nice tutorial , just to add <a href="http:
View Tutorial          By: Rajiv at 2011-12-01 08:43:32

57. its really a good post... very informative..
View Tutorial          By: javagava at 2011-12-13 19:49:08

58. good tutorial
View Tutorial          By: mohang at 2011-12-24 05:24:56

59. Good Explanation of code. But object declaration i
View Tutorial          By: Nitesh at 2011-12-28 12:37:09

60. thanks excellent explanation!!!!!!!!
View Tutorial          By: divya at 2012-01-01 17:37:32

61. good explanation,
Thanks

View Tutorial          By: venkatesh at 2012-01-04 08:43:48

62. I would like to know how to obtain the object that
View Tutorial          By: Steve at 2012-01-18 15:58:31

63. You've explained it excelent
Tanks a lot

View Tutorial          By: Mich at 2012-01-18 16:05:34

64. It is so clear. Thanks for this material.
View Tutorial          By: John Ortiz at 2012-02-26 01:48:32

65. Many thanks for your nice explanation.
View Tutorial          By: Zahra at 2012-04-10 06:03:56

66. superb superb maza aggaya maza aggaya
View Tutorial          By: pramodh at 2012-04-16 05:21:54

67. List<string> al = new List<string>() {
View Tutorial          By: .net whit LinQ gonna change your life.. at 2012-04-23 15:10:55

68. Thanks for giving list iterator example.
it

View Tutorial          By: sandeepika at 2012-05-04 15:57:42

69. Thanks a Lot for gud explaination
View Tutorial          By: Mahi at 2012-06-23 06:01:51

70. Very good program to understand a very tough matte
View Tutorial          By: Arin at 2012-08-08 06:50:42

71. Very usefull info.. Thank alot
View Tutorial          By: Srikanth at 2012-09-04 15:44:30

72. I am able to iterate the list.Now i want to print
View Tutorial          By: akanksha at 2012-09-26 05:25:12

73. explanation is good but i have one doubt what is t
View Tutorial          By: sulthan at 2012-10-09 06:13:14

74. tanx...
View Tutorial          By: sevda at 2013-06-30 21:48:38

75. Thanks i had problem with this util module.
View Tutorial          By: james thompson at 2013-09-08 22:25:50

76. Very nice tutorial demonstrating iteration of arra
View Tutorial          By: akshay at 2013-10-08 08:42:09

77. Very nice tutorial demonstrating iteration of arra
View Tutorial          By: akshay at 2013-10-08 08:43:40

78. Nice one.your example proves that if you are apply
View Tutorial          By: Nisarg pathak at 2015-09-30 17:23:37

79. Do you mind if I quote a couple of your articles a
View Tutorial          By: Lap dance Barcelona at 2017-07-02 19:24:20

80. I see you don't monetize your site, don't waste yo
View Tutorial          By: 86Ronda at 2017-07-27 06:29:44