The differences of Iterator and Iterable interface

淩亂°似流年 2022-02-09 12:39 336阅读 0赞

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 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 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

发表评论

表情:
评论列表 (有 0 条评论,336人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IteratorIterable接口

    Java为了方便编程,预定义了一些特定功能的接口,本文旨在保存和说明这些接口的功能和作用,也会尽量配合源码进行说明,这个会分成多篇文章进行说明,希望大家能够从中获得自己想要的知