Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Scala Notes arrangement (6): Scala Collection Library

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

[TOC]

The main set structure of Scala

1. The collection system in Scala mainly includes: Iterable, Seq (IndexSeq), Set (SortedSet), Map (SortedMap). Where Iterable is the root trait of all collection trait. In fact, Seq, Set, and Map are all sub-trait

Seq

Is a sequence of ordered values, such as an array or list. IndexSeq allows us to quickly access any element through the table below. For example, ArrayBuffer is subscript, but linked lists are not.

Set

Is a set of values that do not have priority. In SortedSet, elements are accessed in some sort of sorted order.

Map

Is a set of (keys, values) duals. SortedMap accesses the entities in the order of the keys.

2. The set in Scala is divided into mutable and immutable sets, in which the variable set means that the elements of the set can be modified dynamically, while the elements of the immutable set cannot be modified after initialization. Correspond to two packages of scala.collection.mutable and scala.collection.immutable respectively.

3. Seq contains sub-trait such as Range, ArrayBuffer, List and so on. Where Range represents a sequence, you can usually use the syntax of "1 to10" to generate a Range. ArrayBuffer is similar to ArrayList in Java.

ListList common operation 1

1. In Scala, a list is either a Nil (and an empty table), or a head element plus a tail, and tail is a list

Scala > val list = List (1,2,3,4,5) list: List [Int] = List (1,2,3,4,5) scala > list.headres29: Int = 1scala > list.tailres30: List [Int] = List (2,3,4,5) scala > list.isEmptyres31: Boolean = falsescala > list = Nilres32: Boolean = false

Here is an example of using recursion to neutralize the list set:

Def recursion (list: list [int]): Int = {if (list.isEmpty) {return 0 / / use return to end the program explicitly, otherwise 0 is only the return value of the if statement and will not end the program Of course, if you use else without return} list.head + recursion (list.tail)} List common operations 2 add / / add / * A. lists + (B)-- > add another list B at the end of list A to form a new list * A. lists: (B)-- > add another list B to the first pair of list A. To form a new list * A.VERV element: (B)-- > add another list B to the first pair of list A, and form a new list *-* A. + (list)-- > add an element at the end of list A. Form a new collection * A. collect: (element)-- > add an element to the head of list A to form a new collection * A. element: (element)-- > add an element to the head of list A to form a new collection * /

The tests are as follows:

Scala > val left = List (1,2,3,4) left: List [Int] = List (1,2,3,4) scala > val right = List (5,6,7) right: List [Int] = List (5,6,7) scala > left.++ (right) res33: List [Int] = List (1,2,3,4,5,6,7) scala > left.++: (right) res34: List [Int] = List (5,6,7,1,2,3) 4) scala > left.::: (right) res35: List [Int] = List (5,6,7,1,2,3,4) scala > left.:+ (10) res36: List [Int] = List (1,2,3,10) scala > left.+: (10) res38: List [Int] = List (10,1,2,3,4) scala > left.:: (10) res39: List [Int] = List (10,1,2,3) 4) Delete drop (n)-> delete the first n elements of list (the first part starts to be deleted) dropRight (n)-- > delete the last n elements of list (tail starts to delete) dropWhile (p: a = > Boolean)-- > match one by one to remove eligible elements Until the conditions are not met, the subsequent elements are no longer judged.

The tests are as follows:

Scala > val list = List (1,2,3,4,5,6,7) list: List [Int] = List (1,2,3,4,5,6,7) scala > list.drop (2) res52: List [Int] = List (3,4,5,6,7) scala > list.dropRight (3) res53: List [Int] = List (1,2,3,4) scala > list.dropWhile (_ list.dropWhile (_ > 3) / the first element is not qualified The latter will no longer judge. So res55: List [Int] = List (1, 2, 3, 4, 5, 6, 7) modify and query scala > val list = List (1, 2, 3, 4, 5, 6, 7) list: List [Int] = List (1, 2, 3, 4, 5, 6, 7) scala > list (0) res57: Int = 1scala > list (0) = 10:9: error: value update is not a member of List [Int] list (0) = 10 ^

The default List is in:

Scala > Listres59: scala.collection.immutable.List.type = scala.collection.immutable.List$@3e98fd35

So it cannot be modified, but trying to import a modifiable List cannot be imported either:

Scala > import scala.collection.mutable.List:8: error: object List is not a member of package scala.collection.mutable import scala.collection.mutable.List

The reasons are as follows:

Scala lists are similar to arrays, where all elements are of the same type, but they are also different: lists are immutable, values cannot be changed once defined, and then lists have a recursive structure (that is, linked table structures) while arrays are not. List common operations 3scala > val list = List (1,2,3,4,5,6,7) list: List [Int] = List (1,2,3,4,5,6,7) scala > list.take (5) res60: List [Int] = List (1,2,3,4) 5) scala > list.takeWhile (_ list.takeWhile (_ > 3) res62: List [Int] = List () scala > list.mkStringres63: String = 1234567scala > list.count (_% 2 = = 0) res64: Int = 3scala > val fruit = "apples":: ("oranges":: ("pears":: Nil) /:: operator creates a new list from the given header and tail fruit: List [String] = List (apples, oranges, pears) scala > val list1 = List (1,2) 3) list1: List [Int] = List (1,2,3) scala > val list2 = List (4,5,6) list2: List [Int] = List (4,5,6) scala > list1 + + list2 / / + + Operation between two sets res70: List [Int] = List (1,2,3,4,5,6) scala > list1.sumres72: Int = 6Set

1. A dataset is a collection of non-repeating elements. It is futile to try to add existing elements. such as

Def setOps:Unit = {(Set (2p0pl 1) + 1) .foreach (println (_))}

2. Set does not retain the order in which elements are inserted. By default, sets are implemented as hash sets, and their elements are organized according to the values of the hashCode method.

Set (1, 2, 3, 5, 7, 8) .foreach (println (_))

3. The chained hash set can remember the order in which elements are inserted. It maintains a linked list to do this.

Val weeks= scala.collection.mutable.LinkedHashSet ("Mo", "Tu", "We", "Th", "Fr") weeks.foreach (println (_))

4. Access the elements in the sorted order

Scala.collection.immutable.SortedSet (1, 2, 3, 4, 5, 6). Foreach (println (_)) set collection order description

Look directly at the following test code and comments:

Package cn.xpleaf.bigdata.p4.collectionimport scala.collection.mutable.SortedSetobject _ 02CollectionOps {def main (args: Array [String]): Unit = {setOps1} / * instructions on the order of scala collections * Test 1: in ascending order of age, equal in age, descending order of names * Test 2: to compare in ascending order of names, compare in ascending order of age * SortedSet is adding ordinary objects such as Person. Error reporting * No implicit Ordering defined for Person * in order to add elements to an ordered collection, you must make the elements comparable with providing a comparator to the collection * in java, the former requires the class to implement Comparator, while the latter needs to provide a comparator to the collection. * in scala, the former needs to allow the class to extend the characteristics of Ordered. The latter passes an Ordering comparator to the collection * * when both comparisons are implemented Give priority to the comparator of the collection * / def setOps1: Unit = {var set = SortedSet [Person] () (new Ordering [Person] {override def compare (x: Person) Y: Person): Int = {var ret = x.getName.compareTo (y.getName) if (ret = = 0) {ret = x.getAge.compareTo ((y.getAge)} ret}}) set.+= (new Person ("Wang Lipeng", 19) set.+= (new Person ("Feng Jian", 18) set.+= (new Person ("Liu Yinpeng") 15) set.+= (new Person ("Li Xiao", 19)) / / println (set) set.foreach (println (_)}} class Person extends Ordered [Person] {private var name:String = _ private var age:Int = _ def this (name:String, age:Int) {this () this.name = name this.age = age} def getAge = age def getName = name override def toString: String = {s "[$name $age] "} / * * in ascending order by age Same age, name in descending order * ascending order: front compared to later * descending order: later than before * / override def compare (that: Person) = {var ret = this.age.compareTo (that.age) if (ret = = 0) {ret = that.name.compareTo (this.name)} ret}}

The output is as follows:

[Feng Jian, 18] [Liu Yinpeng, 15] [Li Xiaoxiao, 19] [Wang Lipeng, 19] functional programming of sets

1. Functional programming of sets is very important, which is the programming method that we will use every day in our future work.

2. We must fully grasp and understand the meaning of Scala's higher-order functions. Map, flatMap, reduce, reduceLeft, foreach and other functions of Scala's collection class are higher-order functions, because they can accept other functions as parameters (some higher-order functions can refer to the functional programming notes arranged earlier).

3. The use of higher-order functions is also the biggest difference between Scala and Java! Because Java does not have functional programming before 1.8.There are certainly no higher-order functions, and it is certainly impossible to pass a function directly into a method, or let a method return a function.

4. The understanding, mastery and use of Scala higher-order functions can greatly enhance your technology, and it is also one of the most attractive and advantageous functions of Scala.

5, in the Spark source code, there are a large number of functional programming, so you must master in order to understand the spark source code.

The foreach method can apply a function to each element in the collection and produce the resulting collection scala > val names = List ("Peter", "Paul", "Mary") names: List [String] = List (Peter, Paul, Mary) scala > names.map (_ .toUpperCase) .foreach (println (_)) PETERPAULMARYMap function is followed by a = = "scala > List (" Peter "," Paul ") "Mary") .map (_ + "= > name") .foreach (println (_)) Peter=== > namePaul=== > nameMary=== > nameFlatmap divided by spaces scala > List ("PeterGlad", "PaulHello", "MaryYour"). FlatMap (_ .split (")) .foreach (println) PeterGladPaulHelloMaryYour

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report