其他分享
首页 > 其他分享> > 衍生集合

衍生集合

作者:互联网

 1 object Test12_DerivedCollection {
 2   def main(args: Array[String]): Unit = {
 3     val list = List(1, 2 ,3 ,4 ,5, 6)
 4     val list1 = List(1, 2, 3, 11, 22, 33)
 5 
 6     //1、获取集合的头
 7     println(list.head)
 8 
 9     //2、获取集合的尾(头剩下的都是尾)
10     println(list.tail)
11 
12     //3、集合最后一个数据
13     println(list.last)
14 
15     //4、集合初始数据(不包含最后一个)
16     println(list.init)
17 
18     //5、反转
19     println(list.reverse)
20 
21     //6、取前(后)n个元素
22     //前
23     println(list.take(3))
24     //后
25     println(list.takeRight(2))
26 
27     //7、去掉前(后)n个元素
28 
29     println(list.drop(2))
30     println(list.dropRight(3))
31 
32     //8、并集
33     val union = list.union(list1)
34     println("union: " + union)
35 
36     //如果是set做并集,会去重
37     val set = Set(1, 2 ,3 ,4 ,5, 6, 7)
38     val set1 = Set(1, 2, 3, 11, 22, 33, 55)
39     val union1 = set.union(set1)
40     println("union1: " + union1)
41 
42     println("-------------------------------")
43 
44     //9、交集
45     val intersection = list.intersect(list1)
46     println("intersection: " +intersection)
47 
48     println("-----------------------------")
49 
50     //10、差集
51     val diff = list.diff(list1)
52     println("diff:" + diff)
53 
54     val diff1 = list1.diff(list)
55     println("diff1: " + diff1)
56 
57     println("----------------------------------")
58 
59     //11、拉链
60     println("zip: " + list.zip(list1))
61     println("zip: " + list1.zip(list))
62 
63     println("----------------------------------")
64 
65     //12、滑窗
66     //窗口的大小为3
67    for (elem <- list.sliding(3)) println(elem)
68     println("----------------------------")
69     //隔两个滑窗一次
70    for (elem <- list1.sliding(4, 2)) println(elem)
71     println("-----------------------")
72     //滚动窗口,窗口大小和相隔数相同
73    for (elem <- list1.sliding(3, 3)) println(elem)
74   }
75 }

 

标签:val,union,list1,list,println,集合,diff,衍生
来源: https://www.cnblogs.com/xiao-wang-tong-xue/p/16287438.html