Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Iterator

Java Iterator

Java Collections Framework At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList.

The Java iterator (Iterator) is a mechanism in the Java Collections Framework; it is an interface used to traverse collections (such as lists, sets, and maps).

It provides a unified way to access elements in a collection without needing to know the specific implementation details of the underlying collection.

The Java Iterator is not a collection; it is a method for accessing collections, and can be used to iterate over ArrayList and HashSet and other collections.

Iterator is the simplest implementation of the Java iterator; ListIterator is an interface in the Collection API that extends the Iterator interface.

The iterator interface defines several methods; the three most commonly used are:

The Iterator class is in the java.util package and must be imported before use, as shown in the following syntax:

import java.util.Iterator; // 引入 Iterator 类

By using an iterator, we can access the elements in a collection one by one without using a traditional for loop or index. This approach is more concise and flexible, and applies to various types of collections.

Obtaining an Iterator

To obtain an iterator from a collection, you can use the iterator() method:

Example

// Import the ArrayList and Iterator classes

import

java.util.ArrayList

;

import

java.util.Iterator

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a collection

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Zhihu"

)

;

       

// Get the iterator

        Iterator

<

String

it

=

sites.

iterator

(

)

;

       

// Output the first element in the collection

       

System

.

out

.

println

(

it.

next

(

)

)

;

   

}

}

After executing the above code, the output is as follows:

Google

When traversing a collection with an iterator, if the collection is modified during traversal (for example, adding or removing elements), it may cause a ConcurrentModificationException. To avoid this problem, you can use the iterator's own remove() method to perform the removal.

Looping Over Collection Elements

The simplest way to let the iterator it return all elements in the collection one by one is to use a while loop:

while(it.hasNext()) {
    System.out.println(it.next());
}

The following outputs all elements in the collection sites:

Example

// Import the ArrayList and Iterator classes

import

java.util.ArrayList

;

import

java.util.Iterator

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a collection

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Zhihu"

)

;

       

// Get the iterator

        Iterator

<

String

it

=

sites.

iterator

(

)

;

       

// Output all elements in the collection

       

while

(

it.

hasNext

(

)

)

{

           

System

.

out

.

println

(

it.

next

(

)

)

;

       

}

   

}

}

After executing the above code, the output is as follows:

Google
Runoob
Taobao
Zhihu

Delete elements

To remove elements from a collection, you can use the remove() method.

In the following example we remove elements less than 10 from the collection:

Example

// Import the ArrayList and Iterator classes

import

java.util.ArrayList

;

import

java.util.Iterator

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

Integer

numbers

=

new

ArrayList

<

Integer

(

)

;

        numbers.

add

(

12

)

;

        numbers.

add

(

8

)

;

        numbers.

add

(

2

)

;

        numbers.

add

(

23

)

;

        Iterator

<

Integer

it

=

numbers.

iterator

(

)

;

       

while

(

it.

hasNext

(

)

)

{

           

Integer

i

=

it.

next

(

)

;

           

if

(

i

<

10

)

{

 

                it.

remove

(

)

;

 

// Remove elements less than 10

           

}

       

}

       

System

.

out

.

println

(

numbers

)

;

   

}

}

After executing the above code, the output is as follows:

[12, 23]

**Note:** The Java iterator is a one-way traversal mechanism, meaning it can only traverse the elements in a collection from front to back, not backward. Also, when using an iterator to traverse a collection, you cannot directly modify the elements in the collection; instead, you must use the iterator's remove() method to delete the current element.

Java Collections Framework At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList.