Standard iterator

Report a typo

As you know, the java.util package includes a standard Iterator<E> interface which has two abstract methods: boolean hasNext() and E next(), as well as two default methods: void remove() and void forEachRemaining(Consumer<? super E> action). Your task is to create an iterator implementing the Iterator<E> interface for the HyperList class. You must implement both abstract methods and override the void remove() method so that it will remove the element returned by the next() method.

Your implementation must comply with the following contract:

  • hasNext() must return true only if there are more elements to iterate.
  • next() must throw NoSuchElementException if the iteration has no more elements.
  • remove() must throw IllegalStateException if the next method has not been called yet.
Write a program in Java 17
import java.util.*;
import java.util.stream.Collectors;

class HyperList<T> {
private final List<T> list;

public HyperList(List<T> list) {
this.list = new ArrayList<>(list);
}

@Override
public String toString() {
return list.toString();
}

// implement this method
public Iterator<T> iterator() {
// TODO
}

// implement the iterator class
class HyperIterator implements Iterator<T> {

@Override
public boolean hasNext() {
// TODO
}

@Override
public T next() {
// TODO
}

@Override
public void remove() {
// TODO
___

Create a free account to access the full topic