Java Core Fundamentals Reveal the core differences between the Iterable interface and the Iterator i

Mondo Technology Updated on 2024-02-07

j**A Core Basics] Reveal the core difference between the iterable interface and the iterator interface! - programmer Goode in j**a,iterableinterface anditeratorInterfaces are all used to iterate through elements in a collection, but they are used differently and function differently.

Official Document Portal:

1. Iterable interface

iterableAn interface is a high-level interface for implementing objects that are capable of performing for-each loops on its elements, and there is only one way for this interface:iterator(), which returns an iterator that iterates on the current collection elementiterableThe main implementation classes of the interface are all collection classes (such as list, set, etc.), which are usediterableThe typical ** is as follows:

list list = new arraylist();

Add the element for (string s : list).

Methods for other collection operations.

override

public iterator iterator()

In this way,mycollectioncan be traversed using a for-each loop, as follows:

mycollection collection = new mycollection<>(

Add elements to the collection

for (string element : collection) {

system.out.println(element);

In this example, the j**a compiler implicitly calls when the for-each loop is executediterator()method to get an iterator and use that iterator to iterate through the elements in the collection.

iteratorThe interface is an iterator for traversing the elements of a collection, and it provides several basic methods that allow sequential access to each element in a collection without knowing the underlying representation of the collection, which is as follows:iteratorMeaning of the main methods in the interface:

hasnext()- This method is used to check if there are more elements in the collection that can be iterated over, and if there are elements behind the current position that the iterator is pointing to, it returnstrue;Otherwise, returnfalsenext()- This method returns the next element that the iterator points to, and moves the position of the iterator forward one element if callednext()When the iterator has reached the end of the collection (iehasnext()Returnfalse), it will be thrownnosuchelementexceptionAbnormal. remove()- This method removes the element from where the iterator last returned, note that it was callednext()After that, it can only be called onceremove()to delete the bynext()Returns the element, if callednext()Called multiple times before or afterremove(), or in the absence of a callnext()in the case of callingremove()), it will be thrownillegalstateexceptionAbnormal. Here's a simple oneiteratorExample of the definition of an interface:

public interface iterator {

boolean hasnext();

e next();

void remove();

When passing through the collectioniterator()method to get oneiteratorobject, it is typically used in the following way:

iterator iterator = collection.iterator();

while (iterator.hasnext())

string element = iterator.next();

Perform operations on the element

If you want, you can delete the current element

iterator.remove();

Follow me and learn Internet programming techniques every day - programmer Goode end!

Related Pages