0

I’m currently working within a java runtime google cloud dataflow. The scala sdk I'm using shows the property I'm working with as an immutable list: https://github.com/snowplow/snowplow-scala-analytics-sdk/blob/master/src/main/scala/com.snowplowanalytics.snowplow.analytics.scalasdk/Event.scala#L91

final List<SelfDescribingData> contexts

Does anyone have any pointers on how to properly cast / convert this to a Java list? Most of the examples I have found are doing this in the Scala runtime vs the Java runtime.

I had thought the JavaConverters package would help me here, however these methods don't seem to be expecting an immutable scala list.

Where e in the example below is an instance of the Event in the linked sdk.

List<SelfDescribingData<Json>> list = JavaConverters.asScalaBufferConverter(e.contexts()).asScala().toList();
for (SelfDescribingData<Json> t : list) {
    LOG.info(t.toString());
}
Jesse
  • 8,223
  • 6
  • 49
  • 81

1 Answers1

3

JavaConverters.asScalaBufferConverter:

Adds an asScala method that implicitly converts a Java List to a Scala mutable Buffer

To convert a scala.collection.immutable.List, which is a subtype of scala.collection.immutable.Seq and scala.collection.Seq to a java.util.List you would call JavaConverters.asJava:

java.util.List<SelfDescribingData<Json>> list = JavaConverters.asJava(e.contexts().data())

The code I've posted is after the Scala 2.13 collection rewrite. For older versions of Scala, the equivalent function is JavaConverters.seqAsJavaList

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30