scala-collections

How to get a random element from a Set in Scala

大兔子大兔子 提交于 2019-12-21 03:18:16
问题 For any given set, for instance, val fruits = Set("apple", "grape", "pear", "banana") how to get a random element from fruits ? Many Thanks. 回答1: convert into Vector and get random element from it scala> val fruits = Set("apple", "grape", "pear", "banana") fruits: scala.collection.immutable.Set[String] = Set(apple, grape, pear, banana) scala> import scala.util.Random import scala.util.Random scala> val rnd=new Random rnd: scala.util.Random = scala.util.Random@31a9253 scala> fruits.toVector

How do I convert an Array[String] to a Set[String]?

旧时模样 提交于 2019-12-21 03:12:31
问题 I have an array of strings. What's the best way to turn it into an immutable set of strings? I presume this is a single method call, but I can't find it in the scala docs. I'm using scala 2.8.1. 回答1: This method called toSet , e.g.: scala> val arr = Array("a", "b", "c") arr: Array[java.lang.String] = Array(a, b, c) scala> arr.toSet res1: scala.collection.immutable.Set[java.lang.String] = Set(a, b, c) In this case toSet method does not exist for the Array . But there is an implicit conversion

How to use Java Collections.shuffle() on a Scala array?

十年热恋 提交于 2019-12-20 19:36:46
问题 I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too: String[] array = new String[]{"a", "b", "c"}; // Shuffle the array; works because the list returned by Arrays.asList() is backed by the array Collections.shuffle(Arrays.asList(array)); I tried using this on a Scala array, but the Scala interpreter responds with a lengthy answer: scala> val a = Array("a", "b", "c")

Compare two Maps in Scala

只谈情不闲聊 提交于 2019-12-20 12:42:07
问题 Is there any pre-defined function that I can use to compare two Maps based on the key and give me the difference? Right now, I iterate Map1 and foreach key, I check if there is an element in Map2 and I pattern match to find the difference. Is there a much elegant way to do this? 回答1: Consider the difference between the maps converted into sets of tuples, (m1.toSet diff m2.toSet).toMap 回答2: Try: val diff = (m1.keySet -- m2.keySet) ++ (m2.keySet -- m1.keySet) diff contains the elements that are

How do I convert a java.util.Map to scala.collection.immutable.Map in Java?

和自甴很熟 提交于 2019-12-20 10:47:35
问题 I find lots of people trying to do this, and asking about this but the question is always answered in terms of scala code. I need to call an API that is expecting a scala.collection.immutable.Map but I have a java.util.Map, how can I cleanly convert from the latter to the former in my java code? The compiler disagrees with the sentiment that it is an implicit conversion as it barfs on that when I try it! Thank you! 回答1: Getting an immutable Scala map is a little tricky because the conversions

Cleaner tuple groupBy

我怕爱的太早我们不能终老 提交于 2019-12-20 09:47:19
问题 I have a sequence of key-value pairs (String, Int), and I want to group them by key into a sequence of values (i.e. Seq[(String, Int)]) => Map[String, Iterable[Int]]) ). Obviously, toMap isn't useful here, and groupBy maintains the values as tuples. The best I managed to come up with is: val seq: Seq[( String, Int )] // ... seq.groupBy( _._1 ).mapValues( _.map( _._2 ) ) Is there a cleaner way of doing this? 回答1: Here's a pimp that adds a toMultiMap method to traversables. Would it solve your

Implicit conversion between java and scala collections using JavaConversions

五迷三道 提交于 2019-12-20 07:32:37
问题 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

split list in scala based on diff between neighbouring elements

天涯浪子 提交于 2019-12-20 06:26:00
问题 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? 回答1: Hmm

How to get a set of all elements that occur multiple times in a list in Scala?

自古美人都是妖i 提交于 2019-12-20 02:34:36
问题 E.g. for List(1, 1, 1, 2, 3, 3, 4) it would be Set(1, 3) , because 1 and 3 are the only elements which occur multiple times. 回答1: val s = List(1, 1, 1, 2, 3, 3, 4) // a list with non-unique elements (s diff s.distinct) toSet // Set(1, 3) 回答2: A bit more convoluted but you can avoid having to call toSet.toList , first group the integers: scala> s.groupBy(identity) res13: scala.collection.immutable.Map[Int,List[Int]] = Map(2 -> List(2), 4 -> List(4), 1 -> List(1, 1, 1), 3 -> List(3, 3)) Then

Underscores and string concatenation in List.map with Scala [duplicate]

≯℡__Kan透↙ 提交于 2019-12-19 11:50:23
问题 This question already has answers here : Scala foreach strange behaviour (5 answers) Closed 5 years ago . Scala lets you use an underscore to do a simple map. So for example instead of writing: def roleCall(people: String*){ people.toList.map(x => println(x)) } ...I can instead write: def roleCall(people: String*){ people.toList.map(println(_)) } However for some reason I can't write: def greet(people: String*){ // This won't compile! people.toList.map(println("Hello " + _)) } instead I have