In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Big data, a good programmer, continues to share the collective operation functions of the Scala series.
4.6 important functions of sets
4.6.1sum/max/min/count
Finding the maximum or minimum value in a sequence is a very common requirement, as follows:
Val numbers = Seq (11,2,5,1,6,3,9)
Numbers.max / / 11
Numbers.min / / 1
A more advanced example that contains a sequence of books
Case class Book (title: String, pages: Int)
Val books = Seq (Book ("Future of Scala developers", 85)
Book (Parallel algorithms, 240)
Book (Object Oriented Programming, 130)
Book (Mobile Development, 495))
/ / Book (Mobile Development,495)
Books.maxBy (book = > book.pages)
/ / Book (Future of Scala developers,85)
Books.minBy (book = > book.pages)
As shown above, the minBy & maxBy method solves the problem of complex data. You only need to choose the attribute that determines the maximum or minimum of the data.
4.6.2 Filterin
Filter a numeric List to get only odd elements.
Val numbers = Seq (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) numbers.filter (n = > n% 2 = 0)
Val books = Seq (Book ("Future of Scala developers", 85)
Book (Parallel algorithms, 240)
Book (Object Oriented Programming, 130)
Book (Mobile Development, 495))
Books.filter (book = > book.pages > = 120)
4.6.3 Flatten
Val abcd = Seq ('averse,' baked, 'clocked,' d')
Val efgj = Seq ('estranged,' faded, 'grubbed,' h')
Val ijkl = Seq ('iTunes,' jacks, 'kicks,' l')
Val mnop = Seq ('masked,' nicked, 'oiled,' p')
Val qrst = Seq ('Q,'r,'s,'t')
Val uvwx = Seq ('upright,' vicious, 'wicked,' x')
Val yz = Seq ('yearly,' z')
Val alphabet = Seq (abcd, efgj, ijkl, mnop, qrst, uvwx, yz)
/ /
/ List (a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, Q, r, s, t
/ / u, v, w, x, y, z)
Alphabet.flatten
Flatten is used when you have a collection of collections and you want to manipulate all the elements of those collections.
4.6.4 Operations between collections
Difference, intersection and union
Val num1 = Seq (1,2,3,4,5,6)
Val num2 = Seq (4,5,6,7,8,9)
/ / List (1,2,3)
Num1.diff (num2)
/ / List (4,5,6)
Num1.intersect (num2)
List (1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9)
Num1.union (num2)
The union in the above example retains the duplicate elements. What if we don't need to repeat it? You can use the distinct function at this point
/ List (1, 2, 3, 4, 5, 6, 7, 8, 9)
Num1.union (num2). Distinct
The following is an illustration of the above functions:
4.6.5 map (Mapping) list elements
Map is one of the most commonly used functions in the Scala collection. It is very powerful:
Val numbers = Seq (1, 2, 3, 4, 5, 6)
/ List (2, 4, 6, 8, 10, 12)
Numbers.map (n = > n * 2)
Val chars = Seq ('averse,' baked, 'clocked,' d')
/ / List (A, B, C, D)
Chars.map (ch = > ch.toUpper)
The logic of the map function is to traverse the elements in the collection and call the function on each element.
4.6.6 flatMap
FlatMap consists of the following two functions:
Map & flatten
Example:
Val abcd = Seq ('averse,' baked, 'clocked,' d')
/ / List (A, a, B, b, C, c, D, d)
Abcd.flatMap (ch = > List (ch.toUpper, ch))
4.6.7 conditional check on the entire collection
Val numbers = Seq (3,7,2,9,6,5,1,4,2) / ture numbers.forall (n = > n
< 10)//false numbers.forall(n =>N > 5)
The forall function is created to deal with such requirements.
4.6.8 grouping collections
For example, if you split a set into even and odd sets, the partition function can help us do this:
Val numbers = Seq (3,7,2,9,6,5,1,4,2)
/ (List (2,6,4,2), List (3,7,9,5,1))
Numbers.partition (n = > n% 2 = = 0)
4.6.9 Fold
Another popular operation is fold, and you can usually consider foldLeft and foldRight. They do the same job in different ways:
Val numbers = Seq (1,2,3,4,5)
/ / 15 numbers.foldLeft (0) ((res, n) = > res + n)
Val words = Seq ("apple", "dog", "table")
/ / 13 words.foldLeft (0) ((resultLength, word) = > resultLength + word.length)
FoldLeft, reduceRight, and foldRight
The method foldLeft works much like reduceLeft, but it lets you specify a value as the first element.
Scala > val a = Array (1,2,3)
A: Array [Int] = Array (1,2,3)
Scala > a.reduceLeft (+)
Res6: Int = 6
Scala > a.foldLeft (100)
Res7: Int = 106
Scala > a.foldLeft (200)
Res8: Int = 206,
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.