One, there are several concepts that you must know when using the Scala collection:
1. What is a predicate?
A predicate is a method, a function or an anonymous function that takes one or more functions and returns a Boolean value.
For example: The following method returns TRUE or FALSE, so it is a predicate.
if true Else false IsEven: (i:int) Boolean
2. What is an anonymous function?
An anonymous function is also called a function argument, which is passed as a variable into a method that takes a function as a parameter, or assigns a value to a variable.
Scala> (i:int) = i% 2 ==0= Boolean = <function1>Scala> val list = List.range (1,10< c3>= List (1, 2, 3, 4, 5, 6, 7, 8, 9) Scala> Val ebents = List.filter (_% 2 = =0 = list (2, 4, 6, 8)
3. What is the privacy cycle?
In the example above, the filter method contains a loop that iterates through each element in the collection and then returns a new collection.
for { | e <- list if e% 2 ==0 | = List (2, 4, 6, 8)
II. hierarchical structure of collections
1. Collection classes
- The traversable trait traverses the entire set, and Scaladoc says he implements a common method for all the collections in terms of the Foreach method, so that the collection can be iterated over and over.
- The iterable trait defines an iterator that can loop through a collection element at a time, but when used with Iterators, the collection is only allowed to be looped once, because each element is changed during the iterator.
2. Sequence
The sequence is divided into two main categories, index sequence and linear sequence.
3.Map class
A mutable map is not in a mutable range by default, so it must be referenced by him.
scala> val m = collection.mutable.Map (1 "A", 2 "b"= Map (2, B, 1 a)
4.Set class
Scala's set is a collection of non-repeating elements.
- If you only need immutable collections, you can create them so that you do not need import imports.
Scala> val set = set (= = Set (1, 2, 3)
- If you need a mutable collection, you must reference it, or refer to the original path.
scala> val set = Collection.mutable.Set (All-in-a = set (1, 2, 3)
third, select a collection class
- scala Universal sequence set
- main immutable sequence set class
- main variable sequence collection class
- Common features in API libraries
- common map , including mutable and immutable versions
- Common set, including mutable and immutable versions
- Other collection class