1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.util; 19 20 /** 21 * An iterator over a sequence of objects, such as a collection. 22 * 23 * <p>If a collection has been changed since the iterator was created, 24 * methods {@code next} and {@code hasNext()} may throw a {@code ConcurrentModificationException}. 25 * It is not possible to guarantee that this mechanism works in all cases of unsynchronized 26 * concurrent modification. It should only be used for debugging purposes. Iterators with this 27 * behavior are called fail-fast iterators. 28 * 29 * <p>Implementing {@link Iterable} and returning an {@code Iterator} allows your 30 * class to be used as a collection with the enhanced for loop. 31 * 32 * @param <E> 33 * the type of object returned by the iterator. 34 */ 35 public interface Iterator<E> { 36 /** 37 * Returns true if there is at least one more element, false otherwise. 38 * @see #next 39 */ hasNext()40 public boolean hasNext(); 41 42 /** 43 * Returns the next object and advances the iterator. 44 * 45 * @return the next object. 46 * @throws NoSuchElementException 47 * if there are no more elements. 48 * @see #hasNext 49 */ next()50 public E next(); 51 52 /** 53 * Removes the last object returned by {@code next} from the collection. 54 * This method can only be called once between each call to {@code next}. 55 * 56 * @throws UnsupportedOperationException 57 * if removing is not supported by the collection being 58 * iterated. 59 * @throws IllegalStateException 60 * if {@code next} has not been called, or {@code remove} has 61 * already been called after the last call to {@code next}. 62 */ remove()63 public void remove(); 64 } 65