The collection hierarchy and classification of Scala's use of multiple collections (1)

Source: Internet
Author: User
Tags map class
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.

    • The index sequence means that random access is the most efficient, such as reading an array of elements, such as arr (5000). By default, in the scala2.10.x version, creating a vector is considered an index sequence.
      scala> val x = indexedseq (All-in-a = Vector (1, 2, 3)
    • The linear sequence description set can be easily distinguished as the head and tail parts, and it is very common to use the tail and IsEmpty methods. A list of lists is created when a linearseq is created.
      scala> val seq = scala.collection.immutable.LinearSeq (All-in-a = List (1, 2, 3)

3.Map class

    • Scala's map is a combination of key-value pairs where the key must be unique. When you only need a simple immutable map, you can create a new one without the need for an import.
      Scala> val m = map (1, "a", 2 "b"= map (1-A, 2-B)
    • 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
    1. scala Universal sequence set
    2. main immutable sequence set class
    3. main variable sequence collection class

    4. Common features in API libraries
    5. common map , including mutable and immutable versions
    6. Common set, including mutable and immutable versions
    7. Other collection class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.