I
I
iscateli2021-12-19 21:00:22
Java
iscateli, 2021-12-19 21:00:22

When to use Collection and when to use Iterator?

In which cases should you go through the Collection, and in which cases through the Iterator? (especially if you just need to run through all the elements). Bruce Eckel in the book "Java Philosophy" (p. 354) writes that

The Iterator solution looks attractive when writing a class where the implementation of the Collection interface is difficult or impractical.

Is this relevant in the modern version of Java as well?
Code example:
//:holding/InterfaceVsIterator.java
import typeinfo.pets.*;
import java.util.*;

public class InterfaceVsIterator{

public static void display(Iterator<Pet> it){
  while(it.hasNext()){
  Pet p=it.next();
  System.out.print(p.id()+":"+p+"");
  }
 System.out.println();
}

public static void display(Collection<Pet> pets){
for(Pet p:pets)
  System.out.print(p.id()+":"+p+"");
System.out.println();
}
...


And why did he name the class InterfaceVsIterator? Iteratorit's also an interface.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vamp, 2021-12-20
@iscateli

In which cases should you go through the Collection, and in which cases through the Iterator? (especially if you just need to run through all the elements).

Depends on the object you just want to run over. If it implements the Iterable interface (or a Collection inherited from it), then for(Pet p:pets) is your option.
You should go through Iterator if you want to iterate over the elements and, in the process of jogging , remove some elements from the iterable collection. This can be done safely only through Iterator.
The Iterator solution looks attractive when writing a class where the implementation of the Collection interface is difficult or impractical.

Here the author means if you yourself write a class that you can "run through". In this case, the easiest way to do this is through an iterator, since it has fewer methods that need to be implemented compared to the Collection interface.

D
Dmitry Roo, 2021-12-19
@xez

Try to never use for and while loops.
Use the Stream API instead.

I
Ilya Plotnikov, 2021-12-19
@fzn7

Be guided by the principle not to produce entities unnecessarily. If in the example above you just need to iterate through all the elements, then with the for each syntax, the iterator is redundant
. The class is so named because the hardest thing in programming is giving the entity the right name. Obviously, the author had not yet mastered this discipline at that time;)

M
mailtime, 2021-12-20
@mailtime

in the case of an iterator:
the Iterator parameter and the input argument:

  1. collection.->call the iterator method. list.iterator()
  2. array. ->get array as stream and call iterator Arrays.stream(array).iterator()

a mutable collection when iterating over an iterator, you can delete/update an element.
immutable: Set.of() and so on, Arrays.asList(array). or Arrays.stream(array)
********************************************** ******************************************************* ***************************************************
in in the case of a collection:
parameter collection:
incoming argument collection.
no element(s) can be inserted/removed from any incoming collection.
updating an element is possible if the collection is mutable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question