The differences of Iterator and Iterable interface

本文详细介绍了Java中Iterator和Iterable两个接口的区别。从抽象方法、使用场景及实现类等方面进行对比,帮助读者理解何时及如何使用这些接口。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值