[置顶] 【scala 代码片段3】轻松入门scala--集合的操作交并补
2017-12-18 15:35
411 查看
主要内容:
1、集合的过滤filter
2、集合的集合flatten
3、集合的交并补diff、intersect、union、distinct
4、flatMap、Map用法
5、集合条件检查
6、集合分组 partition
7、foldLeft求和
代码片段:
运行结果:
1、集合的过滤filter
2、集合的集合flatten
3、集合的交并补diff、intersect、union、distinct
4、flatMap、Map用法
5、集合条件检查
6、集合分组 partition
7、foldLeft求和
代码片段:
/** * Created by Administrator on 2017/1/13. */ object student { def main(args: Array[String]) { // 过滤过集合 val numbers = Seq(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val a = numbers.filter(n => n % 2 == 0) val b = numbers.filter(_ % 2 == 0) println(a) println(b) //然后加大难度,我想获取页数大于120页的书。 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)) val c = books.filter(book => book.pages >= 120) println(c) //当有一个集合的集合,然后你想对这些集合的所有元素进行操作时,就会用到 flatten。 val abcd = Seq('a', 'b', 'c', 'd') val efgj = Seq('e', 'f', 'g', 'h') val ijkl = Seq('i', 'j', 'k', 'l') val mnop = Seq('m', 'n', 'o', 'p') val qrst = Seq('q', 'r', 's', 't') val uvwx = Seq('u', 'v', 'w', 'x') val yz = Seq('y', 'z') val alphabet = Seq(abcd, efgj, ijkl, mnop, qrst, uvwx, yz) val d = alphabet.flatten println(d) val num1 = Seq(1, 2, 3, 4, 5, 6) val num2 = Seq(4, 5, 6, 7, 8, 9) //List(1, 2, 3) // 只在第一个序列出现 println(num1.diff(num2)) //List(4, 5, 6) // 两个集合的交集 println(num1.intersect(num2)) // 两个集合的并集去重 //List(1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9) println(num1.union(num2).distinct) // map(映射)列表元素 val numbers101 = Seq(1, 2, 3, 4, 5, 6) //List(2, 4, 6, 8, 10, 12) println(numbers101.map(m => m * 2)) val chars = Seq('a', 'b', 'c', 'd') //List(A, B, C, D) println(chars.map(ch => ch.toUpper)) // flatMap val abcd2 = Seq('a', 'b', 'c', 'd') //List(A, a, B, b, C, c, D, d) val abcd3 = abcd2.flatMap(ch => List(ch.toUpper, ch)) println(abcd3) //对整个集合进行条件检查 val numbers1 = Seq(3, 7, 2, 9, 6, 5, 1, 4, 2) println(numbers1.forall(m => m < 10)) println(numbers1.forall(m => m > 5)) //partition 对集合进行分组 val numbers3 = Seq(3, 7, 2, 9, 6, 5, 1, 4, 2) //(List(2, 6, 4, 2), List(3, 7, 9, 5, 1)) val number2 = numbers3.partition(n => n % 2 == 0) println(number2) // foldLeft val words = Seq("apple", "dog", "table") println(words.foldLeft(0)((resultLength, word) => resultLength + word.length)) } }
运行结果:
List(2, 4, 6, 8, 10) List(2, 4, 6, 8, 10) List(Book(Parallel algorithms,240), Book(Object Oriented Programming,130), Book(Mobile Development,495)) 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) List(1, 2, 3) List(4, 5, 6) List(1, 2, 3, 4, 5, 6, 7, 8, 9) List(2, 4, 6, 8, 10, 12) List(A, B, C, D) List(A, a, B, b, C, c, D, d) true false (List(2, 6, 4, 2),List(3, 7, 9, 5, 1)) 13 Process finished with exit code 0
相关文章推荐
- [置顶] 【scala 代码片段2】轻松入门scala--列表的操作以及集合元素映射
- [置顶] 【scala 代码片段1】轻松入门scala--列表的操作以及文件读取
- Scala强大的集合数据操作代码示例
- 用代码说话: 比较ruby和scala的集合操作each map filter reduce
- Scala入门系列(十):函数式编程之集合操作
- Scala入门--集合的使用及对集合操作的函数的使用
- 代码片段 - Golang 实现集合操作
- Windows Azure Marketplace入门教学-通过代码操作DataMarket数据源
- Scala 中 10 个超赞的集合操作函数
- jQuery 操作Xml 入门级代码
- 10个超棒jQuery表单操作代码片段
- 第22节:Scala集合和高级函数操作
- android开发中代码片段集合
- MongoDB学习日记 - java代码(三):数据库与集合操作
- scala中集合的操作符号的区别
- 10个jQuery表单操作代码片段
- AutoHotkey/AutoIt自动化操作轻松入门系列
- 10个超棒jQuery表单操作代码片段
- List的一阶函数操作代码实战详解之Scala学习笔记-24
- Scala 深入浅出实战经典 第40讲:Set、Map、TreeSet、TreeMap操作代码实战