scala中优先采用不可变集,List是Nil、有head加上tail
scala> val list = List(1,2,3,4,5) list: List[Int] = List(1, 2, 3, 4, 5) scala> list.getClass res3: Class[_ <: List[Int]] = class scala.collection.immutable.$colon$colon
head取出第一个元素
scala> list.head res4: Int = 1
headOption当List是Nil时有用
scala> val list3 = Nil list3: scala.collection.immutable.Nil.type = List() scala> list3.head java.util.NoSuchElementException: head of empty list at scala.collection.immutable.Nil$.head(List.scala:420) ... 32 elided scala> list3.headOption res26: Option[Nothing] = None
tail取出不含第一个元素的列表
scala> list.tail res5: List[Int] = List(2, 3, 4, 5) scala> list.tail.tail.tail.tail res6: List[Int] = List(5) scala> list.tail.tail.tail.tail.tail res7: List[Int] = List() scala> list.tail.tail.tail.tail.tail.tail java.lang.UnsupportedOperationException: tail of empty list at scala.collection.immutable.Nil$.tail(List.scala:422) at scala.collection.immutable.Nil$.tail(List.scala:417) ... 32 elided
tials
scala> list.tails res9: Iterator[List[Int]] = non-empty iterator scala> list.tails.foreach(println) List(1, 2, 3, 4, 5) List(2, 3, 4, 5) List(3, 4, 5) List(4, 5) List(5) List()
reverse
scala> list.reverse res28: List[Int] = List(5, 4, 3, 2, 1) scala> list res29: List[Int] = List(1, 2, 3, 4, 5)
indexOf
scala> list.indexOf def indexOf[B >: Int](elem: B,from: Int): Int def indexOf[B >: Int](elem: B): Int scala> list.indexOf(3) res30: Int = 2 scala> list.indexOf(3,3) res31: Int = -1
sortBy
scala> val list = List(3,2,5,1,4) list: List[Int] = List(3, 2, 5, 1, 4) scala> list.sortBy def sortBy[B](f: Int => B)(implicit ord: scala.math.Ordering[B]): List[Int] scala> list.sortBy(+_) res37: List[Int] = List(1, 2, 3, 4, 5) scala> list.sortBy(-_) res38: List[Int] = List(5, 4, 3, 2, 1) scala> list.sortBy(_) <console>:13: error: missing parameter type for expanded function ((x$1) => list.sortBy(x$1)) list.sortBy(_) ^
scala> val list2 = List("q","qweee","ee","eee") list2: List[String] = List(q, qweee, ee, eee) scala> list2.sortBy(_.length) res40: List[String] = List(q, ee, eee, qweee) scala> list2.sortBy(x=>x) res43: List[String] = List(ee, eee, q, qweee)
sortWith
scala> list res48: List[Int] = List(3, 2, 5, 1, 4) scala> list.sortWith def sortWith(lt: (Int, Int) => Boolean): List[Int] scala> list.sortWith((a,b) => a > b) //降序 res49: List[Int] = List(5, 4, 3, 2, 1) scala> list.sortWith((a,b) => a < b) //升序 res50: List[Int] = List(1, 2, 3, 4, 5) scala> list.sortWith((a,b) => a == b) //不变 res51: List[Int] = List(3, 2, 5, 1, 4)
sorted升序
scala> list.sorted def sorted[B >: Int](implicit ord: scala.math.Ordering[B]): List[Int] scala> list.sorted res52: List[Int] = List(1, 2, 3, 4, 5)
take
scala> list.take override def take(n: Int): List[Int] scala> list.take(2) //取出前n个元素 res53: List[Int] = List(3, 2) scala> list res54: List[Int] = List(3, 2, 5, 1, 4)
slice
scala> list.slice override def slice(from: Int,until: Int): List[Int] scala> list.sorted.slice(2,3) res55: List[Int] = List(3) scala> list.sorted.slice(2,5) res56: List[Int] = List(3, 4, 5)
toArray
scala> list.toArray def toArray[B >: Int](implicit evidence$1: scala.reflect.ClassTag[B]): Array[B] scala> list.toArray res57: Array[Int] = Array(3, 2, 5, 1, 4)
groupBy
scala> val list = List(2,4,3,2,4,5,6,5,5) list: List[Int] = List(2, 4, 3, 2, 4, 5, 6, 5, 5) scala> list.groupBy(+_) res73: scala.collection.immutable.Map[Int,List[Int]] = Map(5 -> List(5, 5, 5), 6 -> List(6), 2 -> List(2, 2), 3 -> List(3), 4 -> List(4, 4)) scala> list.groupBy(x => if(x%2==0)"even" else "odd") res77: scala.collection.immutable.Map[String,List[Int]] = Map(odd -> List(3, 5, 5, 5), even -> List(2, 4, 2, 4, 6))
map和flapMap对比
scala> val lst = List("hello ni hao","hello en","en ne") lst: List[String] = List(hello ni hao, hello en, en ne) scala> lst.map(_.split(" +")) res105: List[Array[String]] = List(Array(hello, ni, hao), Array(hello, en), Array(en, ne)) scala> lst.flatMap(_.split(" +")) res106: List[String] = List(hello, ni, hao, hello, en, en, ne)
可变列表
scala> import scala.collection.mutable.MutableList import scala.collection.mutable.MutableList scala> val lst = MutableList(3,2,4) lst: scala.collection.mutable.MutableList[Int] = MutableList(3, 2, 4)
scala> lst += 5 res65: lst.type = MutableList(3, 2, 4, 5) scala> lst res66: scala.collection.mutable.MutableList[Int] = MutableList(3, 2, 4, 5) scala> lst ++= (1 to 2) res67: lst.type = MutableList(3, 2, 4, 5, 1, 2) scala> lst res68: scala.collection.mutable.MutableList[Int] = MutableList(3, 2, 4, 5, 1, 2)