Java / Kotlin- Akka流Source.reduce如果Source中为null,则不起作用

问题描述 投票:2回答:1

在Akka Streams Doku中,给出了一个Scala示例,其中包含一个空值的列表。该列表转换为Source,并像示例中那样减少。不幸的是,在Java / Kotlin中,这不会输出任何内容。

通过鸣叫链接到Scala示例:https://doc.akka.io/docs/akka/current/stream/stream-quickstart.html#first-steps

这里是我的科特林翻译

val system = ActorSystem.create("reactive-tweets")

val ints: Source<Int, NotUsed> = Source.from(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, null))
ints
  .filter {
    it != null
  }
  .map {
    it * 2
  }
  .reduce { arg1, arg2 ->
    arg1 + arg2
  }
  .runWith(Sink.foreach { a -> println("sum: " + a)}, system)

我也尝试过Source<Int?, NotUsed>,但没有进行任何更改。我想知道后端是否发生错误或发生了什么,表明流没有到达print语句。

java scala kotlin akka akka-stream
1个回答
0
投票

Akka Streams不允许null元素。记录在https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html#illegal-stream-elements

在该示例中看到的不是null作为流元素,而是Nil被用于使用::运算符构造列表。在Scala中,Nil是包含空列表的常量,::在列表前添加元素。在此示例中,使用它们来构造一个链表,然后将其转换为Source

© www.soinside.com 2019 - 2024. All rights reserved.