<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Scala提供了一套很好的集合實現,提供了一些集合類型的抽象。這讓你的代碼可以與`Foo`的集合交互,而無需擔心該集合是是一個`List`,還是`Set`,或是任何你有的類型。 [這里](http://www.decodified.com/scala/collections-api.xml)?提供了一個很好的頁面來查看各種集合的默認實現,并鏈接到他們的scala在線文檔。 [TOC=2,2] ## 基礎知識 ### 表 List 標準的鏈表。 ~~~ scala> List(1, 2, 3) res0: List[Int] = List(1, 2, 3) ~~~ 你可以用函數式語言的方式連接它們。 ~~~ scala> 1 :: 2 :: 3 :: Nil res1: List[Int] = List(1, 2, 3) ~~~ **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/immutable/List.html) ### 集 Set 集沒有重復 ~~~ scala> Set(1, 1, 2) res2: scala.collection.immutable.Set[Int] = Set(1, 2) ~~~ **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/immutable/Set.html) ### 序列 Seq 序列有一個給定的順序。 ~~~ scala> Seq(1, 1, 2) res3: Seq[Int] = List(1, 1, 2) ~~~ (請注意返回的是一個列表。因為`Seq`是一個特質;而列表是序列的很好實現。如你所見,`Seq`也是一個工廠單例對象,可以用來創建列表。) **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/Seq.html) ### 映射 Map 映射是鍵值容器。 ~~~ scala> Map('a' -> 1, 'b' -> 2) res4: scala.collection.immutable.Map[Char,Int] = Map((a,1), (b,2)) ~~~ **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/immutable/Map.html) ## 層次結構 下面介紹的都是特質,它們在可變的(mutable)和不可變的(immutable)的包中都有特定實現。 ### Traversable 所有集合都可以被遍歷。這個特質定義了標準函數組合子。 這些組合子根據?`foreach`?來寫,所有集合必須實現。 **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/Traversable.html) ### Iterable `iterator()`方法返回一個Iterator來迭代元素。 **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/Iterable.html) ### Seq 序列 有順序的對象序列。 **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/Seq.html) ### Set 集 沒有重復的對象集合。 **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/immutable/Set.html) ### Map 鍵值對。 **參考**?[API文檔](http://www.scala-lang.org/api/current/scala/collection/immutable/Map.html) ## 方法 ### Traversable 下面所有方法在子類中都是可用的。參數和返回值的類型可能會因為子類的覆蓋而看起來不同。 ~~~ def head : A def tail : Traversable[A] ~~~ 這里是函數組合子定義的地方。 `def map [B] (f: (A) => B) : CC[B]` 返回每個元素都被?`f`?轉化的集合 `def foreach[U](f: Elem => U): Unit` 在集合中的每個元素上執行?`f`?。 `def find (p: (A) => Boolean) : Option[A]` 返回匹配謂詞函數的第一個元素 `def filter (p: (A) => Boolean) : Traversable[A]` 返回所有匹配謂詞函數的元素集合 劃分: `def partition (p: (A) ? Boolean) : (Traversable[A], Traversable[A])` 按照謂詞函數把一個集合分割成兩部分 `def groupBy [K] (f: (A) => K) : Map[K, Traversable[A]]` 轉換: 有趣的是,你可以轉換集合類型。 ~~~ def toArray : Array[A] def toArray [B >: A] (implicit arg0: ClassManifest[B]) : Array[B] def toBuffer [B >: A] : Buffer[B] def toIndexedSeq [B >: A] : IndexedSeq[B] def toIterable : Iterable[A] def toIterator : Iterator[A] def toList : List[A] def toMap [T, U] (implicit ev: <:<[A, (T, U)]) : Map[T, U] def toSeq : Seq[A] def toSet [B >: A] : Set[B] def toStream : Stream[A] def toString () : String def toTraversable : Traversable[A] ~~~ 把映射轉換為一個數組,您會得到一個鍵值對的數組。 ~~~ scala> Map(1 -> 2).toArray res41: Array[(Int, Int)] = Array((1,2)) ~~~ ### Iterable 添加一個迭代器的訪問。 ~~~ def iterator: Iterator[A] ~~~ 一個迭代器能給你提供什么? ~~~ def hasNext(): Boolean def next(): A ~~~ 這是非常Java式的。你通常不會看到在Scala中使用迭代器,通常更容易出現的是函數組合器或for循環的使用。 ### Set ~~~ def contains(key: A): Boolean def +(elem: A): Set[A] def -(elem: A): Set[A] ~~~ ### Map 通過鍵查找的鍵值對的序列。 可以像這樣將一個鍵值對列表傳入apply() ~~~ scala> Map("a" -> 1, "b" -> 2) res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((a,1), (b,2)) ~~~ 或者像這樣: ~~~ scala> Map(("a", 2), ("b", 2)) res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((a,2), (b,2)) ~~~ ###### 題外話 什么是`->`?這不是特殊的語法,這是一個返回元組的方法。 ~~~ scala> "a" -> 2 res0: (java.lang.String, Int) = (a,2) ~~~ 請記住,這僅僅是下面代碼的語法糖 ~~~ scala> "a".->(2) res1: (java.lang.String, Int) = (a,2) ~~~ 您也可以使用`++`操作符構建 ~~~ scala> Map.empty ++ List(("a", 1), ("b", 2), ("c", 3)) res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((a,1), (b,2), (c,3)) ~~~ ### 常用的子類 **HashSet和HashMap**?的快速查找,這些集合的最常用的形式。?[HashSet?API](http://www.scala-lang.org/api/current/scala/collection/immutable/HashSet.html),?[HashMap?API](http://www.scala-lang.org/api/current/scala/collection/immutable/HashMap.html) **TreeMap**?是SortedMap的一個子類,它可以讓你進行有序訪問。?[TreeMap?API](http://www.scala-lang.org/api/current/scala/collection/immutable/TreeMap.html) **Vector**?快速隨機選擇和快速更新。?[Vector?API](http://www.scala-lang.org/api/current/scala/collection/immutable/Vector.html) ~~~ scala> IndexedSeq(1, 2, 3) res0: IndexedSeq[Int] = Vector(1, 2, 3) ~~~ **Range**?等間隔的Int有序序列。你經常會在for循環看到。?[Range?API](http://www.scala-lang.org/api/current/scala/collection/immutable/Range.html) ~~~ scala> for (i <- 1 to 3) { println(i) } 1 2 3 ~~~ Ranges支持標準的函數組合子。 ~~~ scala> (1 to 3).map { i => i } res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3) ~~~ ### 默認實現 使用特質的apply方法會給你默認實現的實例,例如,Iterable(1, 2)會返回一個列表作為其默認實現。 ~~~ scala> Iterable(1, 2) res0: Iterable[Int] = List(1, 2) ~~~ 序列Seq也是一樣的,正如我們前面所看到的 ~~~ scala> Seq(1, 2) res3: Seq[Int] = List(1, 2) scala> Iterable(1, 2) res1: Iterable[Int] = List(1, 2) scala> Sequence(1, 2) warning: there were deprecation warnings; re-run with -deprecation for details res2: Seq[Int] = List(1, 2) ~~~ Set ~~~ scala> Set(1, 2) res31: scala.collection.immutable.Set[Int] = Set(1, 2) ~~~ ### 一些描述性的特質 **IndexedSeq**?快速隨機訪問元素和一個快速的長度操作。"API 文檔":http://www.scala-lang.org/api/current/scala/collection/IndexedSeq.html **LinearSeq**?通過head快速訪問第一個元素,也有一個快速的tail操作。?[API文檔](http://www.scala-lang.org/api/current/scala/collection/LinearSeq.html) #### 可變 vs 不可變 不可變 優點 * 在多線程中不會改變 缺點 * 一點也不能改變 Scala允許我們是務實的,它鼓勵不變性,但不懲罰我們需要的可變性。這和var vs. val非常相似。我們總是先從val開始并在必要時回退為var。 我們贊成使用不可改變的版本的集合,但如果性能使然,也可以切換到可變的。使用不可變集合意味著你在多線程不會意外地改變事物。 ## 可變集合 前面討論的所有類都是不可變的。讓我們來討論常用的可變集合。 **HashMap**?定義了?`getOrElseUpdate`,?`+=`?[HashMap?API](http://www.scala-lang.org/api/current/scala/collection/mutable/HashMap.html) ~~~ scala> val numbers = collection.mutable.Map(1 -> 2) numbers: scala.collection.mutable.Map[Int,Int] = Map((1,2)) scala> numbers.get(1) res0: Option[Int] = Some(2) scala> numbers.getOrElseUpdate(2, 3) res54: Int = 3 scala> numbers res55: scala.collection.mutable.Map[Int,Int] = Map((2,3), (1,2)) scala> numbers += (4 -> 1) res56: numbers.type = Map((2,3), (4,1), (1,2)) ~~~ **ListBuffer和ArrayBuffer**?定義?`+=`?[ListBuffer?API](http://www.scala-lang.org/api/current/scala/collection/mutable/ListBuffer.html),?[ArrayBuffer?API](http://www.scala-lang.org/api/current/scala/collection/mutable/ArrayBuffer.html) **LinkedList and DoubleLinkedList**?[LinkedList?API](http://www.scala-lang.org/api/current/scala/collection/mutable/LinkedList.html),?[DoubleLinkedList?API](http://www.scala-lang.org/api/current/scala/collection/mutable/DoubleLinkedList.html) **LinkedList和DoubleLinkedList**?[LinkedList?API](http://www.scala-lang.org/api/current/scala/collection/mutable/LinkedList.html),?[DoubleLinkedList?API](http://www.scala-lang.org/api/current/scala/collection/mutable/DoubleLinkedList.html) **PriorityQueue**?[API?文檔](http://www.scala-lang.org/api/current/scala/collection/mutable/PriorityQueue.html) **Stack 和 ArrayStack**?[Stack?API](http://www.scala-lang.org/api/current/scala/collection/mutable/Stack.html),?[ArrayStack?API](http://www.scala-lang.org/api/current/scala/collection/mutable/ArrayStack.html) **StringBuilder**?有趣的是,StringBuilder的是一個集合。?[API文檔](http://www.scala-lang.org/api/current/scala/collection/mutable/StringBuilder.html) ## 與Java生活 您可以通過[JavaConverters package](http://www.scala-lang.org/api/current/index.html#scala.collection.JavaConverters$)輕松地在Java和Scala的集合類型之間轉換。它用`asScala`?裝飾常用的Java集合以和用`asJava`?方法裝飾Scala集合。 ~~~ import scala.collection.JavaConverters._ val sl = new scala.collection.mutable.ListBuffer[Int] val jl : java.util.List[Int] = sl.asJava val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala assert(sl eq sl2) ~~~ 雙向轉換: ~~~ scala.collection.Iterable <=> java.lang.Iterable scala.collection.Iterable <=> java.util.Collection scala.collection.Iterator <=> java.util.{ Iterator, Enumeration } scala.collection.mutable.Buffer <=> java.util.List scala.collection.mutable.Set <=> java.util.Set scala.collection.mutable.Map <=> java.util.{ Map, Dictionary } scala.collection.mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap ~~~ 此外,也提供了以下單向轉換 ~~~ scala.collection.Seq => java.util.List scala.collection.mutable.Seq => java.util.List scala.collection.Set => java.util.Set scala.collection.Map => java.util.Map ~~~ Built at?[@twitter](http://twitter.com/twitter)?by?[@stevej](http://twitter.com/stevej),?[@marius](http://twitter.com/marius), and?[@lahosken](http://twitter.com/lahosken)?with much help from?[@evanm](http://twitter.com/evanm),?[@sprsquish](http://twitter.com/sprsquish),?[@kevino](http://twitter.com/kevino),?[@zuercher](http://twitter.com/zuercher),?[@timtrueman](http://twitter.com/timtrueman),?[@wickman](http://twitter.com/wickman), and[@mccv](http://twitter.com/mccv); Russian translation by?[appigram](https://github.com/appigram); Chinese simple translation by?[jasonqu](https://github.com/jasonqu); Korean translation by?[enshahar](https://github.com/enshahar); Licensed under the?[Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0).
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看