Scala provides a nice set of collection implementations. It also provides some abstractions for collection types. This allows you to write code that can work with a collection of Foos without worrying whether that collection is a List, Set, or what-have-you. (Notice that returned a List. Seq is a trait, List is a lovely implementation of Seq. There’s a factory object Seq which, as you see here, creates Lists.) These are all traits, both the mutable and immutable packages have implementations of these as well as specialized implementations. All collections can be traversed. This trait defines standard function combinators. These combinators are written in terms of foreach, which collections must implement. All of these methods below are available all the way down. The argument and return types types won’t always look the same as subclasses are free to override them. This is very Java-esque. You often won’t see iterators used in Scala, you are much more likely to see the functional combinators or a for-comprehension used. HashSet and HashMap Quick lookup, the most commonly used forms of these collections. HashSet API, HashMap API Range Ordered sequence of Ints that are spaced apart. You will often see this used where a counting for-loop was used before. Range API Using apply methods on the traits will give you an instance of the default implementation, For instance, Iterable(1, 2) returns a List as its default implementation. LinearSeq fast access only to the first element via head, but also has a fast tail operation. API doc Scala allows us to be pragmatic, it encourages immutability but does not penalize us for needing mutability. This is very similar to var vs. val. We always start with val and move back to var when required. We favor starting with the immutable versions of collections but switching to the mutable ones if performance dictates. Using immutable collections means you won’t accidentally change things in multiple threads. All of the above classes we’ve discussed were immutable. Let’s discuss the commonly used mutable collections. You can easily move between Java and Scala collection types using conversions that are available in the JavaConverters package. It decorates commonly-used Java collections with asScala methods and Scala collections with asJava methods. Built at @twitter by @stevej, @marius, and @lahosken with much help from @evanm, @sprsquish, @kevino, @zuercher, @timtrueman, @wickman, and @mccv, Russian translation by appigram, Chinese simple translation by jasonqu, Korean translation by enshahar, Licensed under the Apache License v2.0. Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: April 18, 2016 @ 3:03 pm