scala-java-interop

Scala Option(null) expected as None but I got Some(0)

浪子不回头ぞ 提交于 2020-06-08 16:26:07
问题 val i: java.lang.Integer = null val o: Option[Int] = Option(i) // This yields Some(0) What is the safe way to convert null: java.lang.Integer to Scala Option[Int] ? 回答1: You are mixing Int and java.lang.Integer so val i: java.lang.Integer = null val o: Option[Int] = Option(i) implicitly converts to val o: Option[Int] = Option(Integer2int(i)) which becomes val o: Option[Int] = Option(null.asInstanceOf[Int]) thus val o: Option[Int] = Some(0) If you want to work with java.lang.Integer , then

Problems with Scala Iterator vs. Java Iterator (a casting nightmare): How to cast to Java from Scala?

好久不见. 提交于 2020-02-07 07:56:15
问题 What I am trying to accomplish is simple: Return the list containing a Tuple2 as a Java Iterator. The code snippet is: ....public java.util.Iterator<Tuple2<Path, Text>> call(InputSplit inputSplit, java.util.Iterator<Tuple2<LongWritable, Text>> iter) throws Exception { ..... java.util.List<Tuple2<Path, Text>> elements = new java.util.ArrayList<Tuple2<Path, Text>>(); while(iter.hasNext()){ Tuple2<LongWritable,Text> tupled = iter.next(); Tuple2<Path, Text> toAdd = new Tuple2<Path, Text>(file

Problems with Scala Iterator vs. Java Iterator (a casting nightmare): How to cast to Java from Scala?

被刻印的时光 ゝ 提交于 2020-02-07 07:54:11
问题 What I am trying to accomplish is simple: Return the list containing a Tuple2 as a Java Iterator. The code snippet is: ....public java.util.Iterator<Tuple2<Path, Text>> call(InputSplit inputSplit, java.util.Iterator<Tuple2<LongWritable, Text>> iter) throws Exception { ..... java.util.List<Tuple2<Path, Text>> elements = new java.util.ArrayList<Tuple2<Path, Text>>(); while(iter.hasNext()){ Tuple2<LongWritable,Text> tupled = iter.next(); Tuple2<Path, Text> toAdd = new Tuple2<Path, Text>(file

Using Scala traits with implemented methods in Java

故事扮演 提交于 2020-01-26 09:40:41
问题 I guess it is not possible to invoke methods implemented in Scala traits from Java, or is there a way? Suppose I have in Scala: trait Trait { def bar = {} } and in Java if I use it as class Foo implements Trait { } Java complains that Trait is not abstract and does not override abstract method bar() in Trait 回答1: Answer From Java perspective Trait.scala is compiled into Trait interface . Hence implementing Trait in Java is interpreted as implementing an interface - which makes your error

Using Scala traits with implemented methods in Java

元气小坏坏 提交于 2020-01-26 09:40:19
问题 I guess it is not possible to invoke methods implemented in Scala traits from Java, or is there a way? Suppose I have in Scala: trait Trait { def bar = {} } and in Java if I use it as class Foo implements Trait { } Java complains that Trait is not abstract and does not override abstract method bar() in Trait 回答1: Answer From Java perspective Trait.scala is compiled into Trait interface . Hence implementing Trait in Java is interpreted as implementing an interface - which makes your error

Using Scala traits with implemented methods in Java

て烟熏妆下的殇ゞ 提交于 2020-01-26 09:40:05
问题 I guess it is not possible to invoke methods implemented in Scala traits from Java, or is there a way? Suppose I have in Scala: trait Trait { def bar = {} } and in Java if I use it as class Foo implements Trait { } Java complains that Trait is not abstract and does not override abstract method bar() in Trait 回答1: Answer From Java perspective Trait.scala is compiled into Trait interface . Hence implementing Trait in Java is interpreted as implementing an interface - which makes your error

Convert Java Map to Scala Map

不打扰是莪最后的温柔 提交于 2020-01-19 07:05:07
问题 I have a java map: java.util.Map<SomeObject, java.util.Collection<OtherObject>> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]] I have used mapAsScalaMap but the result is not quite what I want, the result is: Map[SomeObject, java.util.Collection[OtherObject]] . How can I fix it to also convert the collection to a set? NOTE: actually my original problem was to convert google's ArrayListMultimap<SomeObject, OtherObject> to a MultiMap[SomeObject, OtherObject]

Convert Java Map to Scala Map

谁说胖子不能爱 提交于 2020-01-19 07:02:31
问题 I have a java map: java.util.Map<SomeObject, java.util.Collection<OtherObject>> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]] I have used mapAsScalaMap but the result is not quite what I want, the result is: Map[SomeObject, java.util.Collection[OtherObject]] . How can I fix it to also convert the collection to a set? NOTE: actually my original problem was to convert google's ArrayListMultimap<SomeObject, OtherObject> to a MultiMap[SomeObject, OtherObject]

Convert Java Map to Scala Map

大兔子大兔子 提交于 2020-01-19 07:02:05
问题 I have a java map: java.util.Map<SomeObject, java.util.Collection<OtherObject>> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]] I have used mapAsScalaMap but the result is not quite what I want, the result is: Map[SomeObject, java.util.Collection[OtherObject]] . How can I fix it to also convert the collection to a set? NOTE: actually my original problem was to convert google's ArrayListMultimap<SomeObject, OtherObject> to a MultiMap[SomeObject, OtherObject]