Big Data series Cultivation-scala course 11

Source: Internet
Author: User

Then yesterday's list, is also the knowledge of learning collection

Listbuffer, ArrayBuffer, Queue, stack related operations

1.ListBuffer, Arraybuffer code implementation: Listbuffer and Arraybuffer are mutable variable and Java definition is similar, can be appended.

2.Queue, stack related operations: The queue is one end into the end, and the stack is the principle of LIFO, they are mutable variable

    ImportScala.collection.mutable.ListBuffer Val Listbuffer=NewListbuffer[int] Listbuffer+ = 1Listbuffer+ = 2println (Listbuffer)//arraybuffer is variable.    ImportScala.collection.mutable.ArrayBuffer Val ArrayBuffer=NewArraybuffer[int] () ArrayBuffer+ = 1ArrayBuffer+ = 2println (ArrayBuffer)//queues are first in, first out, and variableval empty =Queue[int] () Val queue1= Empty.enqueue (1) Val queue2= Queue1.enqueue (List (2,3,4,5) ) println (queue2) val (element, left)=queue2.dequeue println (element+ " : " +Left )Importscala.collection.mutable.Queue Val Queue=queue[string] () Queue+ = "a"Queue++= List ("B", "C") println (queue) println (queue.dequeue) println (queue)//stacks are also variable, advanced and post -    ImportScala.collection.mutable.Stack Val Stack=NewStack[int] Stack.push (1) Stack.push (2) Stack.push (3) println (stack.top) println (Stack) println (stack.pop) println (Stack)
Set, Map, TreeSet, TREEMAP related operations

1.Set, Map related operations: Set and map elements are mutable variable is also unordered

2.TreeSet, TreeMap related operations: TreeMap and TreeSet can be used to sort the

Importscala.collection.mutableImportScala.collection.mutable.TreeSetImportScala.collection.immutable.TreeMapImportscala.collection.immutable.HashMapobject Set_map {def main (args:array[string]) {//set is mutable and unorderedVal data =mutable. Set.empty[int] Data++= List (1, 2, 3) Data+ = 4; Data--= List (2, 3); println (data) data+ = 1; println (data) data.clear println (data)//map is also a mutable unordered map value to the map (key) =valueVal map =mutable. Map.empty[string, String] Map ("Java") = "Hadoop"Map ("Scala") = "Spark"println (map) println (map) ("Scala"))     //TreeSet used to sortVal TreeSet = TreeSet (9, 3, 1, 8, 0, 2, 7, 4, 6, 5) println (TreeSet) Val Treesetforchar= TreeSet ("Spark", "Scala", "Hadoop") println (Treesetforchar)//TreeMap used to sortvar treeMap = TreeMap ("Scala", "Spark", "Java", "Hadoop")) println (TREEMAP)}}
Insider and method operations for the implementation of the list inheritance system

1.List Inheritance system analysis and Source code interpretation: list by inheriting other classes and themselves are abstract class or sealed decorated, with the parameters are generic inherit the parent class

Abstract class extends Abstractseq[a]                                  with Linearseq[a]                                  with Product                                  with Generictraversabletemplate[a, List] with                                  Linearseqoptimized[a, List[a]]                                  
Sealed used for pattern matching means that all implementations under the package can use the
+a: Can be a generic or subclass object pointing to the parent class

2.List method operation and source code interpretation: by implementing Nil and:: Mainly inside the head and tail as well as the IsEmpty function

@SerialVersionUID (0-8256821097970055419l) CaseObject NilextendsList[nothing] {override def isEmpty=trueOverride def head:nothing=Throw NewNosuchelementexception ("Head of empty list") override def Tail:list[nothing]=Throw NewUnsupportedoperationexception ("Tail of the Empty list")  //removal of equals method here might leads to a infinite recursion similar to intmap.equals.Override Def equals (that:any) =That match { CaseThat1:scala.collection.genseq[_] =That1.isempty Case_ =false  }}--main implementation inside head, tail, isempty function, etc.
// value computed by Serialver for 2.11.2, annotation added in 2.11.4 Final  Case class Private extends List[b] {  = tl  false}--mainly implemented:: null implementation
Generics, generic functions, generics in Scala are widely used in spark

Generic type implementations in 1.Scala: Generic types are used quite a lot in Scala

Generic function implementation in 2.Scala

Generics in 3.Scala are widely used in spark

def ZIP[A1;: A, B, that] (That:geniterable[b]) (implicit Bf:canbuildfrom[repr, (A1, B), this]): that =ABS Tractclassextends  abstractseq[a] with                                  Linearseq[a] and                                  Product                                  with Generictraversabletemplate[a, List] with                                  linearseqoptimized[a, List[a]) with                                  Serializable-- Generics are widely used in Scala and can be said to be ubiquitous, and Scala can automatically infer what type of

The above is today's study, not very deep, the sense of application from the Scala source to understand, in order to delve into the things in it.

This content is to learn from Liaoliang Teacher's tutorial, his number: 18610086859

Latest Lesson Videos 77: Http://yun.baidu.com/s/1dD10KFZ

The address of the article 39-42 speaks: http://pan.baidu.com/s/1jGlbG2q

Big Data series cultivation-scala course one by one

Related Article

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.