How to use Iterator in Java

By: Fazal  

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 
// use iterator to display contents of al 
System.out.print("Original contents of al: "); 
Iterator itr = al.iterator(); 
while(itr.hasNext()) {

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

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

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

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

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

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

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



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

Archived Comments

1. I see you don't monetize your site, don't waste your
traffic, you can earn additional bucks

View Tutorial          By: 86Ronda at 2017-07-27 06:29:44

2. Do you mind if I quote a couple of your articles as long as I provide
credit and sources bac

View Tutorial          By: Lap dance Barcelona at 2017-07-02 19:24:20

3. Nice one.your example proves that if you are apply list iterator on collection object than one can c
View Tutorial          By: Nisarg pathak at 2015-09-30 17:23:37

4. Very nice tutorial demonstrating iteration of array list. While i was iterating Set elements. Thanx
View Tutorial          By: akshay at 2013-10-08 08:43:40

5. Very nice tutorial demonstrating iteration of array list. While i was iterating Set elements. Thanx
View Tutorial          By: akshay at 2013-10-08 08:42:09

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

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

8. explanation is good but i have one doubt what is the use of Object in the program
View Tutorial          By: sulthan at 2012-10-09 06:13:14

9. I am able to iterate the list.Now i want to print value on screen.How to do this?
View Tutorial          By: akanksha at 2012-09-26 05:25:12

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

11. Very good program to understand a very tough matter like Iterator.
View Tutorial          By: Arin at 2012-08-08 06:50:42

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

13. Thanks for giving list iterator example.
it was nice and once again thank u

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

14. List<string> al = new List<string>() { "C", "A", "E", &quo
View Tutorial          By: .net whit LinQ gonna change your life.. at 2012-04-23 15:10:55

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

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

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

18. You've explained it excelent
Tanks a lot

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

19. I would like to know how to obtain the object that the Iterator is actually referencing. Not all Ar
View Tutorial          By: Steve at 2012-01-18 15:58:31

20. good explanation,

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

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

22. Good Explanation of code. But object declaration inside while loop is not perfect. People may confus
View Tutorial          By: Nitesh at 2011-12-28 12:37:09

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

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

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

26. Thanks dude....
Good work...

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

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

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

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

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

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

32. good explanation for the java beginners...could you explain about the hasNext(),hasPrevious() method
View Tutorial          By: karthik at 2011-08-19 11:17:53

33. This is Nice start for programmer '
Thanks ..

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

34. Also note that for reverse ordering, If you have a Collection that implements either Deque (like Lin
View Tutorial          By: Dwight at 2011-07-13 22:13:45

35. Nice Example.Good work.keep it up.

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

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

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

38. I am still going through the tutorial, but it seems to trail off or that last sentence is truncated.
View Tutorial          By: shadowwraith at 2011-06-08 08:00:57

39. Great stuff, learned more from this example than reading java books on the same topic for hours.
View Tutorial          By: David Bandelin at 2011-03-22 07:10:38

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

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

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

43. just want to can i use iterator to compare value inside the object...for example..finding h
View Tutorial          By: ikhwan at 2011-02-02 17:14:07

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

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

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

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

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

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

50. good, it clear the confusion......
nice code, but not good to initalize the object in the whi

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

View Tutorial          By: vinay sharma at 2010-10-01 05:16:23

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

53. Good help me to do mi projecto! thanks.
View Tutorial          By: gnk at 2010-08-12 01:01:50

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

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

56. its realy gud which clears minor douts regarding the problems we are facing
View Tutorial          By: Shripad at 2010-01-19 05:40:41

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

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

59. i know this example this is in mg.hill but i want to know that with out set by ListIterator cant we
View Tutorial          By: srikanta at 2009-11-18 00:06:57

60. sir ji,
i want do learn for loop in java and form which i want to create different shapes lik

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

61. The iterator returned by iterator() does not point to start of of collection. It points to head whic
View Tutorial          By: Jeffrey at 2009-10-06 21:45:41

62. could u pls tell me whcih class is implemted by iterator interface...
if any class implements

View Tutorial          By: AMIT at 2009-10-04 00:21:30

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

64. Sorry:

#!/usr/bin/env ruby
al = ["C", "A", "E",

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

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

66. I'm a beginner, and I was told it is a good practice to use:

List <String>list

View Tutorial          By: Nelson at 2009-08-17 09:13:28

67. Thanks, much appreciated!
Saved a great deal of trawling thru- the classes and interfaces in

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

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

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

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

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

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

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

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

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

76. Good explanation about Iterator. But Object declaration inside while loop is not all good style of p
View Tutorial          By: Prasad at 2008-12-22 23:20:59

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

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

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

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

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial