• union
• intersection
• subtract
• distinct
• cartesian
• zip
• union
• intersection
• subtract
• distinct
• cartesian
• zip
> sample
> randomSplit
• map
• filter
• flatMap
• mapPartitions
• mapPartitionsWithIndex
• groupBy
• sortBy
https://sourceforge.net/projects/openart/
To convert data between the flat world of arrays and the recursive world of lists,you can use method toArray in class List and toList in class Array:
object List_Array extends App{
val v1=List(1,2)
val v3=v1.toArray
val v2=Array(3,4)
val v4=v2.toList
println(v3)
println(v4)
}
[I@77b52d12
List(3, 4)
There are also variants of the mkString methods called addString which append the constructed string to a StringBuilder object
The mkString and addString methods are inherited from List’s supertrait Traversable, so they are applicable to all other collections, as well.
object List_StringBuilder extends App{
val lst=List("america","india")
val buf = new StringBuilder
println(lst addString (buf, "(", ";", ")"))
//stringbuilder object
}
(america;india)
The operation xs mkString (pre, sep, post) involves four operands: the list xstobedisplayed,aprefixstringpretobedisplayedinfrontofallelements, a separator string sep to be displayed between successive elements, and a postfix string post to be displayed at the end. The result of the operation is the string:
pre + xs(0) + sep + …+ sep + xs(xs.length – 1) + post
The mkString method has two overloaded variants that let you drop some or all of its arguments. The first variant only takes a separator string:
xs mkString sep equals xs mkString (“”, sep, “”)
The second variant lets you omit all arguments:
xs.mkString equals xs mkString “”
object List_mkstring extends App{
val lst=List(1,2,3)
println(lst.mkString("##","*","&&"))
println(lst mkString "#")
println(lst mkString)
}
##1*2*3&&
1#2#3
123
The zip operation takes two lists and forms a list of pairs:
If the two lists are of different length, any unmatched elements are dropped:
A useful special case is to zip a list with its index. This is done most efficiently with the zipWithIndex method, which pairs every element of a list with the position where it appears in the list.
Any list of tuples can also be changed back to a tuple of lists by using the unzip method:
object List_Zip_Unzip extends App {
// If the two lists are of different length, any unmatched elements are dropped:
val lst=List('a','b','c')
val lstzip=lst.indices zip lst
println(lstzip)
//scala.collection.immutable.IndexedSeq[(Int, Char)]
val lstindex=lst.zipWithIndex
println(lstindex)
// List[(Char, Int)]
println(lstindex.unzip)
} Vector((0,a), (1,b), (2,c)) List((a,0), (b,1), (c,2)) (List(a, b, c),List(0, 1, 2))
flatten method takes a list of lists and flattens it out to a single list:
It can only be applied to lists whose elements are all lists. Trying to flatten any other list will give a compilation error:
object List_Flatten extends App{
val lst = List(List(1, 2), List(3), List(), List(4, 5)).flatten
println(lst)
val ctry=List("america","india")
val chr=ctry.map(_.toCharArray).flatten
println(chr)
}
List(1, 2, 3, 4, 5)
List(a, m, e, r, i, c, a, i, n, d, i, a)
One reason why random element selection is less popular for lists than for arrays is that xs(n) takes time proportional to the index n. In fact, apply is simply defined by a combination of drop and head:
xs apply n equals (xs drop n).head
The indices method returns a list consisting of all valid indices of a given list:
object Lists_Random extends App{
val lst=List(1,2,3,4)
println(lst.apply(1))
println(lst.indices)
}
2
Range(0, 1, 2, 3)
scala.collection.immutable.Range