我是靠谱客的博主 个性手套,这篇文章主要介绍groovy学习之列表操作--操作列表元素,现在分享给大家,希望可以做个参考。

groovy对列表的操作功能实在是强大,简洁的语法让人欲罢不能,闲话少说

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def "过滤列表"() { def ages = [20, 36, 42, 56] def midage = 21..50 expect: ages.grep(midage) == [36, 42] println ages.grep(midage) } def "list of lists,demo flatten"() { def list = [[1, 2], [3, 4, 5]].flatten() expect: list == [1, 2, 3, 4, 5] println list } def "demo list intersect("() { def list1 = [1, 2, 3] def list2 = [2, 3, 4] expect: "求交集" list1.intersect(list2) == [2, 3] } def "demo list disjoint("() { expect: "不交" assert [1, 2, 3].disjoint([4, 5, 6]) } def "demo list pop,like stack("() { def list = [1, 2, 3] def popped = list.pop() expect: popped == 3 list == [1, 2] } def "列表排序和反转"() { assert [1, 2].reverse() == [2, 1] assert [3, 1, 2].sort() == [1, 2, 3] def list = [[1, 0], [0, 1, 2]] when: "根据第一个元素排序,默认升序 asc" list = list.sort { a, b -> a[0] <=> b[0] } then: list == [[0, 1, 2], [1, 0]] when: "根据size大小来排序" list.sort { item -> item.size() } then: list == [[1, 0], [0, 1, 2]] } def "两种方法,列表元素去重"() { def x = [1, 1, 1] expect: assert [1] == new HashSet(x).toList() assert [1] == x.unique() } def "列表去null"() { def x = [1, null, 1] expect: assert [1, 1] == x.findAll { it != null } assert [1, 1] == x.grep { it } }


最后

以上就是个性手套最近收集整理的关于groovy学习之列表操作--操作列表元素的全部内容,更多相关groovy学习之列表操作--操作列表元素内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部