scala-collections

Implicit conversion between java and scala collections using JavaConversions

我是研究僧i 提交于 2019-12-02 13:02:58
I merged a scala Set of scala Map s using a generic function def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] = (Map[A, B]() /: (for (m <- ms; kv <- m) yield kv)) { (a, kv) => a + (if (a.contains(kv._1)) kv._1 -> f(a(kv._1), kv._2) else kv) } This handles the case when there is a clash of same keys. However, I wanted to do it with Java collections in Scala Code. I researched a bit and came across JavaConversions . I imported it and wrote this def mergeMaps[A, B](ms: Set[Map[A, B]])(f: (B, B) => B): Map[A, B] = (new util.HashMap[A, B] /: (for (m <- ms; kv <- m) yield kv)) {

Getting all key value pairs having the maximum value from a Scala map

寵の児 提交于 2019-12-02 11:40:33
问题 I have seen a similar post here here which is giving a single key-value pair which has maximum value in the entire Map. But I would like to get List of pairs which has maximum value(maximum value is same for many pairs). Ex : Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2) Expected Output : List(1 -> 7, 4 -> 7) This ( Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2).maxBy(x => x._2) ) will give only first occurrence 1 -> 7 回答1: Using map.filter(_._2 == map.values.max) will do the trick. 回答2: val maxValue = map.values

Extract numbers from String Array

你。 提交于 2019-12-02 09:25:17
I have a Array of Strings scala> tokens res34: Array[String] = Array(The, value, of, your, profile, is, 234.2., You, have, potential, to, gain, 8.3, more.) Here each of comma separated value is a String. I want to extract numbers from this i.e. my output should be result = (234.2, 8.3) & it should be mutable so that I can read from another array and append values What data structure should I use to achieve this? Consider import scala.util._ tokens.flatMap(s => Try( s.split("\\W+").mkString(".").toDouble ).toOption) where we tokenize further each array string into words and append them by a dot

Scala Seq GroupBy with Future

别来无恙 提交于 2019-12-02 09:20:51
I have 2 case classes case class First(firstId: Long, pt: Long, vt: Long) case class Second(secondId: Int, vt: Long, a: Long, b: Long, c: Long, d: Long) I have one collection (data:Seq[First]). There is one function which transforms this sequence to another Seq[Second] after applying groupBy and one future operation. getFutureInt is some function returns Future[Int] val output: Future[Seq[Second]] = Future.sequence(data.groupBy(d => (d.vt, getFutureInt(d.firstId))).map {case(k, v) => k._2.map { si => Second(si, k._1, v.minBy(_.pt).pt, v.maxBy(_.pt).pt, v.minBy(_.pt).pt, v.maxBy(_.pt).pt)}}

split list in scala based on diff between neighbouring elements

自闭症网瘾萝莉.ら 提交于 2019-12-02 08:22:21
How do we split list in scala based on difference between neighbouring elements. For example given List(1,3,6,10,12,14) and difference 3, the function would return List(List(1,3),List(6),List(10,12,14)). Can we user foldLeft to do that? I was trying to create a function def splitDiff(list:List[Int],diff:Int) = def func(list:List[List[Int]],m:Int):List[List[Int]] = //compare with last element list.foldLeft(List(List(0))).foldLeft(func) But the inner function seems difficult? Any help? Hmm, I have a solution, but I suspect one can do better: (test.head :: test).sliding(2).toList.map( (pair: List

How does Option act like a collection, when it isn't one?

北慕城南 提交于 2019-12-02 07:00:43
问题 The accepted answer to "How to convert a Some(“ ”) to None in one-line?" took the form: def convert(x: Option[String]) : Option[String] = x.map(_.trim()).filterNot(_.isEmpty()) My problem is that I can't figure out how to find by what means the collection returned by filterNot is converted to an Option. I looked at the Scaladoc for Option constructors, Option Object, Predef, Seq, and Seq Object. I figure there's probably an implicit somewhere, but how does one go about finding it? 回答1: In

Override toString in a Scala set

混江龙づ霸主 提交于 2019-12-02 05:04:49
问题 I want to create a set of integers called IntSet . IntSet is identical to Set[Int] in every way except that its toString function prints the elements as comma-delimited (the same as if you called mkString(",") ), and it has a constructor that takes a Traversable of integers. What is the simplest way to do this? > IntSet((1 to 3)).toString 1,2,3 I'd think there would be some one-line way to do this, but I've been fiddling around with implicit functions and extending HashSet and I can't figure

Getting all key value pairs having the maximum value from a Scala map

橙三吉。 提交于 2019-12-02 04:02:16
I have seen a similar post here here which is giving a single key-value pair which has maximum value in the entire Map. But I would like to get List of pairs which has maximum value(maximum value is same for many pairs). Ex : Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2) Expected Output : List(1 -> 7, 4 -> 7) This ( Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2).maxBy(x => x._2) ) will give only first occurrence 1 -> 7 Using map.filter(_._2 == map.values.max) will do the trick. val maxValue = map.values.max map.filter(_._2 == maxValue).toList 来源: https://stackoverflow.com/questions/49792228/getting-all-key-value

How does Option act like a collection, when it isn't one?

限于喜欢 提交于 2019-12-02 03:26:56
The accepted answer to " How to convert a Some(“ ”) to None in one-line? " took the form: def convert(x: Option[String]) : Option[String] = x.map(_.trim()).filterNot(_.isEmpty()) My problem is that I can't figure out how to find by what means the collection returned by filterNot is converted to an Option. I looked at the Scaladoc for Option constructors, Option Object, Predef, Seq, and Seq Object. I figure there's probably an implicit somewhere, but how does one go about finding it? In scaladoc, you can click the "by inheritance" ordering button (it appears just above the methods description).

Override toString in a Scala set

て烟熏妆下的殇ゞ 提交于 2019-12-02 00:08:49
I want to create a set of integers called IntSet . IntSet is identical to Set[Int] in every way except that its toString function prints the elements as comma-delimited (the same as if you called mkString(",") ), and it has a constructor that takes a Traversable of integers. What is the simplest way to do this? > IntSet((1 to 3)).toString 1,2,3 I'd think there would be some one-line way to do this, but I've been fiddling around with implicit functions and extending HashSet and I can't figure it out. The trick is to use a proxy object. Eastsun has the answer below. Here's a slightly different