Scala's collection Collection

Source: Internet
Author: User
Tags spark rdd

Overview

Scala's collection classes can be sliced from three dimensions:

    • Mutable and immutable collections (immutable and mutable collections)
    • Static and Lazy load collection (Eager and delayed evaluation)
    • Serial and parallel compute sets (sequential and parallel evaluation)

About the first dimension I don't think we need to introduce any more. As for the second dimension, it is explained in this way, first of all we explain a concept: transformation, a large number of operations in the collection is to "transform" a set into another set, such as Map,filter and so on. The difference between the eager and delayed collections is that the eager collection always allocates memory for the element immediately, and when a transform action is encountered, the eager collection evaluates directly and returns the result, while the delayed collection delays execution as late as possible. Execution is not performed until the result must be returned. This is very similar to the transformation and action in the Spark RDD operation.

In the existing collection, only the stream is lasy, and all other collections are static (Eager) loaded. But you can easily convert a static collection to lazy, which is to create a view.

Collection type Overview

Immutable Collectionimmutable Seq

Seq is divided into two main categories: indexed sequences and linear sequences,indexed sequences imply that this collection has a higher performance in random reads (similar to arrays in data structures). Linear sequences implies that this collection is more advantageous in head and tail operations and sequential traversal (similar to a bidirectional list in a data structure)

When using SEQ, the specific class used by default is list, and the specific class used by default when using Indexedseq is a vector.

scala> val seq = Seq(1,2,3)seq: Seq[Int] = List(123)scala> val indexedSeq = IndexedSeq(1,2,3)indexedSeq: IndexedSeq[Int] = Vector(123)
Immutable Set

Immutable Map

Mutable Seq

How to select a collection class characteristics comparison of various immutable sequence

Comparison of characteristics of various mutable sequence

Comparison of the characteristics of various maps

Comparison of the characteristics of various set

Note: This class diagram is referenced from "Beginning Scala" and the form is referenced from "Scala Cookbook"

Scala Collection Collection

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.