`
perfy315
  • 浏览: 412145 次
社区版块
存档分类
最新评论

java中的Iterator和Iterable 区别

阅读更多
java.lang.Iterable
java.util.Iterator

来自百度知道:
Iterator是迭代器类,而Iterable是接口。
好多类都实现了Iterable接口,这样对象就可以调用iterator()方法。
一般都是结合着用,比如
HashMap类就实现了Iterable接口,而要访问或打印出Map中所有内容时,就可以这样: HashMap hashMap;
Iterator iter = hashMap.iterator();
while(iter.hashNext()) {
  String s = iter.next();
}

转至:http://liuyun025.iteye.com/blog/1321045
为什么一定要实现Iterable接口,为什么不直接实现Iterator接口呢?
      看一下JDK中的集合类,比如List一族或者Set一族,都是实现了Iterable接口,但并不直接实现Iterator接口。
仔细想一下这么做是有道理的。

      因为Iterator接口的核心方法next()或者hasNext() 是依赖于迭代器的当前迭代位置的。
      如果Collection直接实现Iterator接口,势必导致集合对象中包含当前迭代位置的数据(指针)。
      当集合在不同方法间被传递时,由于当前迭代位置不可预置,那么next()方法的结果会变成不可预知。
      除非再为Iterator接口添加一个reset()方法,用来重置当前迭代位置。
      但即时这样,Collection也只能同时存在一个当前迭代位置。
      而Iterable则不然,每次调用都会返回一个从头开始计数的迭代器。
      多个迭代器是互不干扰的。

分享到:
评论
5 楼 qinghuazangshui 2017-03-27  
[flash=200,200][img][list]
[*]
引用
[u][i][/i][/u]
[/list][/img][/flash]
|
 
4 楼 yangyangrenren 2016-06-26  
Iterator是迭代器类么?源码中是这样的
/**
* Implementing this interface allows an object to be the target of
* the "foreach" statement.
*
* @param <T> the type of elements returned by the iterator
*
* @since 1.5
*/
public interface Iterable<T>
3 楼 guoxin91 2015-03-22  
HashSet倒是实现了Iterator接口。

jdk api1.6中
Iterator所有已知子接口:
BeanContext, BeanContextServices, BlockingDeque<E>, BlockingQueue<E>, Collection<E>, Deque<E>, List<E>, NavigableSet<E>, Queue<E>, Set<E>, SortedSet<E>
Iterator所有已知实现类:
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
2 楼 zymgloria 2014-03-09  
yuanhongb 写道
HashMap类有实现Iterable接口???

一般都是结合着用,比如
HashMap类就实现了Iterable接口,而要访问或打印出Map中所有内容时,就可以这样: HashMap hashMap;
Iterator iter = hashMap.iterator();
while(iter.hashNext()) {
  String s = iter.next();
}



  HashMap不能这么遍历
1 楼 yuanhongb 2013-02-02  
HashMap类有实现Iterable接口???

一般都是结合着用,比如
HashMap类就实现了Iterable接口,而要访问或打印出Map中所有内容时,就可以这样: HashMap hashMap;
Iterator iter = hashMap.iterator();
while(iter.hashNext()) {
  String s = iter.next();
}


相关推荐

Global site tag (gtag.js) - Google Analytics