scala-collections

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]

How to parse JSON with lift-json in Scala?

这一生的挚爱 提交于 2020-01-15 15:46:15
问题 When I am trying parse the json object I am getting the below error. import net.liftweb.json._ object SarahEmailPluginConfigTest { implicit val formats = DefaultFormats case class Mailserver(url: String, username: String, password: String) val json = parse("""{"url": "imap.yahoo.com", "username": "myusername", "password": "mypassword" }""") def main(args: Array[String]) { val m = json.extract[Mailserver] println(m.url) println(m.username) println(m.password) } } I have added "lift-json_2.9.0

How to get to type parameters of a reflect.runtime.universe.Type in scala? [duplicate]

烈酒焚心 提交于 2020-01-15 03:59:26
问题 This question already has answers here : Finding type parameters via reflection in Scala 2.10? (2 answers) Closed 5 years ago . Suppose I get a Type representing List[Int]: > import scala.reflect.runtime.universe > val mirror = universe.runtimeMirror(this.getClass.getClassLoader) mirror: reflect.runtime.universe.Mirror = JavaMirror with ... > class X{ def getList():List[Int] = null } defined class X > val method = mirror. classSymbol(classOf[X]). toType. declarations. filter{_.isMethod}. map{

How do you rotate (circular shift) of a Scala collection

有些话、适合烂在心里 提交于 2020-01-13 08:48:34
问题 I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the following: val seq = Seq(1,2,3,4,5) for (i <- seq.indices) { for (j <- seq.indices) { print(seq(i + j % seq.length)) } } But as I'm looking to fold over the collection, I'm wondering if there is a more idiomatic approach. A recursive approach would allow me to avoid any var s. But basically, I'm wondering if something like the following is

scala - Confusing “diverging implicit expansion” error when using “sortBy”

一曲冷凌霜 提交于 2020-01-12 14:50:27
问题 I wonder why List(3,2,1).toIndexedSeq.sortBy(x=>x) doesn't work: scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong <console>:8: error: missing parameter type List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ <console>:8: error: diverging implicit expansion for type scala.math.Ordering[B] starting with method Tuple9 in object Ordering List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ scala> Vector(3,2,1).sortBy(x=>x) // OK res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> Vector(3,2,1)

Apply function to one element only in list or array in Scala

混江龙づ霸主 提交于 2020-01-12 10:09:07
问题 For any given list or array, for instance val list = (1 to 3).toList val array = (1 to 3).toArray and a given function that maps from and onto the collection type, for instance def f(v: Int): Int = v + 10 how to apply f to the ith element of list or array so that list.myApply(f, ith = 2) res: List(1,12,3) and also array.myApply(f, ith = 2) res: Array(1,12,3) 回答1: tl;dr import scala.collection.SeqLike import scala.collection.generic.CanBuildFrom implicit class Seq_[A, Repr, S : ({type L[X] = X

Combining multiple Lists of arbitrary length

你说的曾经没有我的故事 提交于 2020-01-12 06:54:07
问题 I am looking for an approach to join multiple Lists in the following manner: ListA a b c ListB 1 2 3 4 ListC + # * § % .. .. .. Resulting List: a 1 + b 2 # c 3 * 4 § % In Words: The elements in sequential order, starting at first list combined into the resulting list. An arbitrary amount of input lists could be there varying in length. I used multiple approaches with variants of zip, sliding iterators but none worked and especially took care of varying list lengths. There has to be an elegant

Summing up two options

空扰寡人 提交于 2020-01-12 05:28:04
问题 Let's say I have two optional Ints (both can be Some or None): val one : Option[Int] = Some(1) val two : Option[Int] = Some(2) My question is the following: Are there any intelligent way to sum them op using Scalas brilliant collection-methods? I realize that I could merge them into a collection, flatten it and use reduceLeftOption like so: (one :: two :: Nil).flatten.reduceLeftOption(_ + _) // Some(3) But, the solution above means creating a new collection, and living in a rich and developed

Scala: How to create a Map[K,V] from a Set[K] and a function from K to V?

▼魔方 西西 提交于 2020-01-10 10:14:32
问题 What is the best way to create a Map[K,V] from a Set[K] and function from K to V ? For example, suppose I have scala> val s = Set(2, 3, 5) s: scala.collection.immutable.Set[Int] = Set(2, 3, 5) and scala> def func(i: Int) = "" + i + i func: (i: Int)java.lang.String What is the easiest way of creating a Map[Int, String](2 -> "22", 3 -> "33", 5 -> "55") 回答1: You can use foldLeft : val func2 = (r: Map[Int,String], i: Int) => r + (i -> func(i)) s.foldLeft(Map.empty[Int,String])(func2) This will