The differences of Iterator and Iterable interface
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
Secondly, when to use Iterable.
If you want to reiterate the elements, you must use the Iterable interface.
If you want to use the loop foreach, you must use the Iterable interface. e.g., Iterable
its; for(E it:its) {}, ArrayList als=new ArrayList (); 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
还没有评论,来说两句吧...