

Behavior of the Iterator size method in Scala
source link: https://blog.adamgamboa.dev/behavior-of-the-iterator-size-method-in-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Behavior of the Iterator size method in Scala – Adam Gamboa G – DeveloperSkip to content
Full Stack notes and tips for developers… Java, React, Kubernetes, Scala, and more…
Iterators are a useful structure to iterate over collections (List, Set, Seq, Map, etc) and access their elements one by one. Two important methods in Scala Iterator are next()
and hasNext
.
- hasNext: Indicates if there is another element to return
- next(): Returns that element.
Iterators are mutable things. You can think of them as pointers. Each time you ask an iterator to give you the next element it points to it will move one position further.
Behavior of the size method
When the size of a iterator is requested, it will traverse each element in the sequence it points to, moving each time one position to the right. When it has no more elements to traverse iterator.hasNext == false
it will return the size. But by then it will have exhausted all the elements.
When a new call to size
is made, the iterator is already positioned at the end, so it will immediately return 0. Therefore if you tries to iterate your iterator after getting the size of it, you will notice the iterator is not doing anything.
val list = scala.collection.immutable.List(1, 2, 3) val iterator = list.iterator println(iterator.size) // 3 println(iterator.hasNext) // false println(iterator.size) // 0
val list = scala.collection.immutable.List(1, 2, 3)
val iterator = list.iterator
println(iterator.size) // 3
println(iterator.hasNext) // false
println(iterator.size) // 0
Reference
Recommend
-
181
README.md
-
77
为什么 在很多情况下,集合内容被封装到特定类里,只能通过一些接口来进行访问,而如果是STL容器,则可以利用STL算法进行非常灵活、表达能力更强的操作,譬如如下类: 在对FakeVector进行操作时,只能有如下写法: 而不能像操作STL容器一样使用std::copy和std::tra...
-
119
A high-performance 100% compatible drop-in replacement of "encoding/json" Benchmark
-
60
集合体系中的迭代器就是遍历,集合取出元素的方式,迭代器可以取出并操作集合中的元素迭代器的特点:1每个集合都有存和取的方式,而且每个集合的数据结构不一样,所以他们存和取的动作也不一样,添加通过add()能完成添加操作,但是取的动作比较复杂,所以把取的...
-
6
What is the maximum size of the file that I can download on the azur blob download method advertisements Please can i know what the maximum fi...
-
4
Reading Time: 3 minutes In this blog, we will discuss about Scala fields and methods. As we know that fields in any language used to store the information and methods are use to define the behavior of the object. Let’s discuss more a...
-
6
Jetpack Compose For-loop range must have an ‘iterator()’ method 2021-12-07 在了解了 compose 之后,决定自己撸一个九宫格尝试下绘制以及手势,于是这个过程中发现了一个有意思的事情。先用 IDE 的新建 Empty Compose...
-
14
New issue Add a try_reduce method to the Iterator trait #87054
-
12
Copy link Contributor a-lafrance
-
10
Contributor
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK