本文整理汇总了Scala中scala.collection.Iterator类的典型用法代码示例。如果您正苦于以下问题:Scala Iterator类的具体用法?Scala Iterator怎么用?Scala Iterator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Iterator类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CollectionUtils
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.commons.utils
import kafka.consumer.ConsumerTimeoutException
import scala.collection.{AbstractIterator, Iterator}
object CollectionUtils {
implicit class iteratorUtils[T](iterators: List[Iterator[T]]) {
def merge = new AbstractIterator[T] {
private var inUseItrIndex = 0
private val iteratorSize = iterators.size
private def unsafeHasNext(index: Int) = try { iterators(index).hasNext } catch { case _: ConsumerTimeoutException => false }
override def hasNext: Boolean = {
if (iterators.nonEmpty) {
inUseItrIndex = (1 + inUseItrIndex) % iteratorSize
unsafeHasNext(inUseItrIndex) || {
def r(stopIdx: Int): Boolean = {
inUseItrIndex = (1 + inUseItrIndex) % iteratorSize
if (stopIdx != inUseItrIndex) {
unsafeHasNext(inUseItrIndex) || r(stopIdx)
} else false
}
r(inUseItrIndex)
}
} else false
}
override def next(): T = {
if(iterators.nonEmpty)
iterators(inUseItrIndex).next()
else
Iterator.empty.next()
}
}
}
}
示例2: WrapIterator
//设置package包名称以及导入依赖的类
package scawrap
import scala.collection.Iterator
import scala.scalajs.js
import js.annotation.ScalaJSDefined
@ScalaJSDefined
private[scawrap] class WrapIterator[A](
private val underlying: Iterator[A]) extends js.Iterator[js.UndefOr[A]] {
def next(): js.Iterator.Entry[js.UndefOr[A]] = {
if (underlying.hasNext) {
new js.Iterator.Entry[js.UndefOr[A]] {
val done: Boolean = false
val value = js.defined(underlying.next())
}
} else {
new js.Iterator.Entry[js.UndefOr[A]] {
val done: Boolean = true
val value: js.UndefOr[Nothing] = js.undefined
}
}
}
}