-
def main(args: Array[String]){
-
-
val bigData = List("Hadoop" , "Spark")
-
val data = List(1 , 2 , 3)
-
-
val bigData_Core = "Hadoop" :: ("Spark" :: Nil) //跟List("Hadoop" , "Spark")定义方式结果一样
-
val data_Int = 1 :: 2 :: 3 :: Nil
-
-
println(data.isEmpty)
-
println(data.head)
-
println(data.tail.head)//第二个元素
-
-
val List(a,b) = bigData
-
println("a : " + a + " === " + " b: " + b)
-
val x :: y :: rest = data //rest是剩下的元素所组成的一个子List
-
println("x : " + x + " === " + " y: " + y + " === " + rest )
-
-
val shuffledData = List(6,3,5,6,2,9,1)
-
println(sortList(shuffledData))
-
-
def sortList(list : List[Int]): List[Int] = list match{
-
case List() => List()
-
case head :: tail => compute (head, sortList(tail))
-
}
-
-
//递归排序步骤:
-
//sortList递归到最底层时,head是1,tail是Nil,compute(1, Nil)时返回List<1>
-
//往上,compute(9, List<1>),结果:List<1,9>
-
//往上,compute(2, List<1,9>),结果:List<1,2,9>
-
//...
-
-
def compute(data : Int , dataSet : List[Int]) : List[Int] = dataSet match{
-
case List() => List(data)
-
case head :: tail => if (data <= head) data :: dataSet
-
else head :: compute(data, tail)
-
}
-
-
}
List的一阶函数操作
-
def main(args: Array[String]) {
-
println(List (1,2,3,4) ::: List (4,5,6,7,8) ::: List (10,11)) //将多个List连接在一起(两个冒号是元素与元素、元素与列表的连接)
-
println(List (1,2,3,4) ::: (List (4,5,6,7,8) ::: List (10,11)))
-
println(List (1,2,3,4).length) //该方法比较慢,尽量不用
-
-
val bigData = List("Hadoop" , "Spark" , "Kafka")
-
println(bigData.last) //kafka
-
println(bigData.init) //除了最后一个元素之外的所有元素集合,这里是List("Hadoop", "Spark")
-
println(bigData.reverse)//产生新的List,结果是List("Kafka", "Spark", "Hadoop")
-
println(bigData) //还是原来的List("Hadoop", "Spark", "Kafka")
-
println(bigData take 2) //取前两个元素,List("Hadoop", "Spark")
-
println(bigData drop 2) //去掉前两个元素,List("kafka")
-
println(bigData splitAt 2) //将前两个元素一组,其余元素一组进行划分,(List("Hadoop", "Spark"), List("Kafka"))
-
println(bigData apply 2) //访问index=2的元素,跟下面的结果一样,不常用
-
println(bigData(2))
-
-
val data=List('a' ,'b', 'c', 'd', 'e', 'f')
-
println(data.indices) //返回所有元素的索引,Range(0,1,2,3,4,5)
-
println(data.indices zip data) //将两组元素配对,Vector((0,a),(1,b),(2,c),(3,d),(4,e),(5,f))
-
println(data.zipWithIndex)//将元素与索引配对更简捷写法,List((0,a),(1,b),(2,c),(3,d),(4,e),(5,f))
-
println(data.toString)//List('a' ,'b', 'c', 'd', 'e', 'f')
-
println(data.mkString ("[", ",,", "]"))//将集合按照一定样式输出,[a,,b,,c,,d,,e,,f]
-
println(data.mkString ("******"))//一个参数指分隔符,a******b******c******d******e******f
-
println(data mkString)//输出集合元素,abcdef
-
-
val buffer = new StringBuilder
-
data addString (buffer, "(", ";;", ")")//跟mkString结果一样,只不过是将结果放到buffer中,(a;;b;;c;;d;;e;;f)
-
println(buffer)
-
println(data) //List(a, b, c, d, e, f)
-
-
val array = data.toArray //将List转化为数组
-
println(array.toList) //将数组转化为List,List(a, b, c, d, e, f)
-
-
val new_Array = new Array[Char](10)
-
data.copyToArray(new_Array,3) //将List拷贝至数组,并将数组的前三个位置空出来
-
new_Array.foreach(print)
-
println
-
-
val iterator = data.toIterator //返回List的迭代器
-
println(iterator.next) //a
-
println(iterator.next) //b
-
}
归并排序:
-
object MergedSort {
-
-
def main(args: Array[String]){
-
-
def mergedsort[T] (less: (T, T) => Boolean) (input: List[T]): List[T] = {
-
-
/**
-
* @param xList 要合并的有序列表
-
* @param yList 要合并的有序列表
-
* @return 合并后的列表
-
*/
-
def merge(xList: List[T], yList: List[T]): List[T] =
-
(xList, yList) match {
-
case (Nil, _) => yList
-
case (_, Nil) => xList
-
case (x :: xtail, y :: ytail) => //比较两个List的head,谁小谁放前面,剩下两个List再递归merge
-
if (less(x, y)) x :: merge(xtail, yList)
-
else y :: merge(xList, ytail)
-
}
-
-
val n = input.length / 2
-
if (n == 0) input
-
else {
-
val (x, y) = input splitAt n //把要排序的列表input平均分成两个列表
-
merge(mergedsort(less)(x), mergedsort(less)(y)) //先对分后的两个列表归并排序,再对排好的有序表进行归并
-
}
-
}
-
println(mergedsort((x: Int, y: Int) => x < y) (List (3, 7, 9, 5)))
-
val reversed_mergedsort=mergedsort((x: Int, y: Int) => x > y) _
-
println(reversed_mergedsort(List(3, 7, 9, 5)))
-
-
}
-
}
List的Map、flatMap、froeach、filter操作
-
def main(args: Array[String]) {
-
println(List(1, 2, 3, 4, 6) map (_ + 1)) //map函数作用于List中的每一个元素,结果:List(2, 3, 4, 5, 7)
-
val data = List("Scala", "Hadoop", "Spark")
-
println(data map (_.length)) //结果:List(5, 6, 5)
-
println(data map (_.toList.reverse.mkString)) //结果:List(alacS, poodaH, krapS)
-
-
println(data.map(_.toList)) //结果:List(List(S, c, a, l, a), List(H, a, d, o, o, p), List(S, p, a, r, k))
-
println(data.flatMap(_.toList)) //将map操作产生的结果合并,产生新的集合,结果:List(S, c, a, l, a, H, a, d, o, o, p, S, p, a, r, k)
-
println(List.range(1, 10) flatMap (i => List.range(1, i) map (j => (i, j))))
-
-
var sum = 0
-
List(1, 2, 3, 4, 5) foreach (sum += _) //对每个元素进行没有结果的函数操作
-
println("sum : " + sum)
-
-
println(List(1, 2, 3, 4, 6, 7, 8, 9, 10) filter (_ % 2 ==0))
-
println(data filter (_.length == 5)) //过滤出符合要求的元素集合
-
-
-
println(List(1, 2, 3, 4, 5) partition (_ % 2 ==0)) //把元素按照某种条件分裂,结果:(List(2, 4),List(1, 3, 5))
-
println(List(1, 2, 3, 4, 5) find (_ % 2 ==0)) //找出集合中第一个满足条件的元素,结果:Some(2),因为有可能找不到,所以是Optional的子类Some
-
println(List(1, 2, 3, 4, 5) find (_ <=0)) //没有结果,所以是None
-
println(List(1, 2, 3, 4, 5) takeWhile (_ < 4))//获取符合条件的元素,结果:List(1, 2, 3)
-
println(List(1, 2, 3, 4, 5) dropWhile (_ < 4)) //把符合条件的元素去掉,结果:List(4, 5)
-
println(List(1, 2, 3, 4, 5) span (_ < 4)) //把元素按条件分裂成不同部分,结果:(List(1, 2, 3),List(4, 5))
-
-
def hastotallyZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0))//forall,只有集合中所有元素都满足条件才返回true;exists,只要有一个元素满足条件就返回true
-
val m= List(List(1,0,0), List(0,0,0), List(0,0,1))
-
println(hastotallyZeroRow(m))
-
-
}
foldLeft、foldRight、sortWith
-
def main(args: Array[String]){
-
println((1 to 5).foldLeft(0)(_+_) )//List从左往右,依次相加,0位初始值,0+1+2+3+4+5
-
println((0 /: (1 to 5))(_+_)) //也可以这么写:\:
-
-
println((1 to 5).foldRight(1)(_-_)) //初始值为1,从右往左,依次相减5-1 = 4,然后4-4=0,3-0=3,2-3=-1,1-(-1)=2
-
println(((1 to 5):\1)(_-_))
-
-
-
println(List(1, -3, 4, 2, 6) sortWith (_ < _))
-
println(List(1, -3, 4, 2, 6) sortWith (_ > _))
-
-
}
List伴生对象
-
def main(args: Array[String]) {
-
println(List.apply(1, 2, 3)) //返回一个List(1,2,3),底层调用toList方法
-
println(List.make(3, 5)) //构造一个具有重复元素的List,第一个参数是元素重复的次数,第二个参数是元素的内容,结果:List(5,5,5)
-
println(List.range(1, 5)) //生成一个List,左闭右开,List(1,2,3,4)
-
println(List.range(9, 1, -3))//指定步长,List(9,6,3)
-
-
val zipped = "abcde".toList zip List(1, 2, 3, 4, 5)
-
println(zipped) //将两个List组合起来,List((a,1), (b,2), (c,3), (d,4), (e,5))
-
println(zipped.unzip) //和zip操作相反,结果:(List(a, b, c, d, e),List(1, 2, 3, 4, 5))
-
-
println(List(List('a', 'b'), List('c'), List('d', 'e')).flatten)//将所有子List构成一个大的List,结果:List(a, b, c, d, e)
-
println(List.concat(List(), List('b'), List('c'))) //也是构成一个大的List,结果:List(b, c)
-
-
println(List.map2(List(10, 20), List(10, 10)) (_ * _)) //将两个List进行函数操作,结果:List(100, 200)
-
-
}
List继承体系分析以及方法操作源码解读
-
sealed abstract class List[+A] extends AbstractSeq[A] //sealed常用来进行模式匹配
-
with LinearSeq[A]
-
with Product
-
with GenericTraversableTemplate[A, List]
-
with LinearSeqOptimized[A, List[A]] {
-
override def companion: GenericCompanion[List] = List
-
-
import scala.collection.{Iterable, Traversable, Seq, IndexedSeq}
-
-
def isEmpty: Boolean
-
def head: A
-
def tail: List[A]
-
-
...
-
}
* This class comes with two implementing case classes `scala.Nil`
* and `scala.::` that implement the abstract members `isEmpty`,
* `head` and `tail`.
List有两个实现类,Nil与::
子对象Nil的定义:
-
case object Nil extends List[Nothing] { //空对象,全局唯一
-
override def isEmpty = true
-
override def head: Nothing =
-
throw new NoSuchElementException("head of empty list")
-
override def tail: List[Nothing] =
-
throw new UnsupportedOperationException("tail of empty list")
-
// Removal of equals method here might lead to an infinite recursion similar to IntMap.equals.
-
override def equals(that: Any) = that match {
-
case that1: scala.collection.GenSeq[_] => that1.isEmpty
-
case _ => false
-
}
-
}
子类::的定义:
-
final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extends List[B] {
-
override def head : B = hd
-
override def tail : List[B] = tl
-
override def isEmpty: Boolean = false
-
-
private def readObject(in: ObjectInputStream) {
-
val firstObject = in.readObject()
-
hd = firstObject.asInstanceOf[B]
-
assert(hd != ListSerializeEnd)
-
var current: ::[B] = this
-
while (true) in.readObject match {
-
case ListSerializeEnd =>
-
current.tl = Nil
-
return
-
case a =>
-
val list : ::[B] = new ::(a.asInstanceOf[B], Nil)
-
current.tl = list
-
current = list
-
}
-
}
-
-
private def writeObject(out: ObjectOutputStream) {
-
var xs: List[B] = this
-
while (!xs.isEmpty) { out.writeObject(xs.head); xs = xs.tail }
-
out.writeObject(ListSerializeEnd)
-
}
-
}
List的构造是的类型约束逆变、协变、下界
-
abstract class Big_Data
-
class Hadoop extends Big_Data
-
class Spark extends Big_Data
-
-
object List_Constructor_Internals {
-
def main(args: Array[String]){
-
val hadoop = new Hadoop :: Nil //等价于Nil.::(new Hadoop),结果:hadoop:List[Hadoop] = List(com.dt.scala.list.Hadoop@2666e815),new Hadoop为List的head,Nil为List 的tail
-
val big_Data = new Spark :: hadoop //等价于hadoop.::(new Spark),结果:big_Data:List[Big_data] = List(com.dt.scala.list.Spark@4e106082, com.dt.scala.list.Hadoop@2666e815)
-
}
-
-
//::源码
-
def ::[B >: A] (x: B): List[B] =
new scala.collection.immutable.::(x, this)
-
-
//看了::源码之后就会明白,往List[hadoop]里面.::(new Spark)怎么会返回List[Big_data]了
-
}
ListBuffer高效遍历
-
object ListBuffer_Internals {
-
def main(args: Array[String]) {
-
val list = List(1,2,3,4,5,6,7,8,9)
-
increment(list)
-
increment_MoreEffective(list)
-
increment_MostEffective(list)
-
}
-
-
def increment(list: List[Int]): List[Int] = list match {
-
case List() => List()
-
case head :: tail => head + 1 :: increment(tail)
-
}
-
-
def increment_MoreEffective(list: List[Int]): List[Int] = {
-
var result = List[Int]()
-
for(element <- list) result = result ::: List(element +1)
-
result
-
}
-
-
def increment_MostEffective(list: List[Int]): List[Int] = {
-
import scala.collection.mutable.ListBuffer
-
var buffer = new ListBuffer[Int]
-
for(element <- list) buffer += element + 1
-
buffer.toList
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-1870473/
,如需转载,请注明出处,否则将追究法律责任。