This post will describe som differences of Iterator and Iterable interface. Introduction with three aspects is given out.
Firstly, relative abstract method.
Iterator interface:
boolean hasNext(); // return true if there are elements to iterate.
E next(); //return the next element of iterator.
void remove(); //remove the last element of iterator.
Iterable interface:
Iterator<E> Iterator();//return an iterator with a group elements of data type E.
Secondly, when to use Iterable.
1. If you want to reiterate the elements, you must use the Iterable interface.
2. If you want to use the loop foreach, you must use the Iterable interface. e.g., Iterable<E> its; for(E it:its) {}, ArrayList<T> als=new ArrayList<T>(); for(T al:als){}
! foreach only uses the array or the instance of Iterable.
Thirdly, all the classes implement the Iterable interface.
AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BatchUpdateException, BeanContextServicesSupport, BeanContextSupport, ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DataTruncation, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, RowSetWarning, SerialException, ServiceLoader, SQLClientInfoException, SQLDataException, SQLException, SQLFeatureNotSupportedException, SQLIntegrityConstraintViolationException, SQLInvalidAuthorizationSpecException, SQLNonTransientConnectionException, SQLNonTransientException, SQLRecoverableException, SQLSyntaxErrorException, SQLTimeoutException, SQLTransactionRollbackException, SQLTransientConnectionException, SQLTransientException, SQLWarning, Stack, SyncFactoryException, SynchronousQueue, SyncProviderException, TreeSet, Vector
Fourthly, all the classes implement the Iterator interface.
BeanContextSupport.BCSIterator, EventReaderDelegate, Scanner