迭代器性能合同(并用于非收集) [英] Iterator performance contract (and use on non-collections)

查看:110
本文介绍了迭代器性能合同(并用于非收集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你正在做的只是一次简单的一次迭代(即只有 hasNext() next(),没有 remove()),您是否保证每次操作的线性时间性能和/或摊销的固定成本?

If all that you're doing is a simple one-pass iteration (i.e. only hasNext() and next(), no remove()), are you guaranteed linear time performance and/or amortized constant cost per operation?

这是在 Iterator 合同中指定的吗?

Is this specified in the Iterator contract anywhere?

是否存在数据结构/ Java Collection 哪些不能在线性时间内迭代?

Are there data structures/Java Collection which cannot be iterated in linear time?

java.util.Scanner实现Iterator< String> 扫描仪几乎不是数据结构(例如 remove()绝对没有意义)。这被认为是一个设计错误吗?

java.util.Scanner implements Iterator<String>. A Scanner is hardly a data structure (e.g. remove() makes absolutely no sense). Is this considered a design blunder?

类似于 PrimeGenerator实现了Iterator< Integer> 被认为是糟糕的设计,或者是这正是 Iterator 的用途? ( hasNext()始终返回true, next()按需计算下一个数字, remove()没有意义。)

Is something like PrimeGenerator implements Iterator<Integer> considered bad design, or is this exactly what Iterator is for? (hasNext() always returns true, next() computes the next number on demand, remove() makes no sense).

同样,对于 java.util.Random实现它是否有意义Iterator< Double>

一个类型真的应该实现 Iterator 如果它只是有效的使用其三分之一的API? (即没有 remove(),总是 hasNext()

Should a type really implement Iterator if it's effectively only using one-third of its API? (i.e. no remove(), always hasNext())

推荐答案

没有这样的保证。正如你所指出的,任何人都可以将任何东西建模为Iterator。迭代器的各个生产者必须指定他们的个人表现。

There is no such guarantee. As you point out, anyone can model anything as Iterator. Individual producers of iterators would have to specify their individual performance.

这篇关于迭代器性能合同(并用于非收集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆