groovy-集合

Lists

你能使用下面的方法建立一個lists,注意[]是一個空list。html

1 def list = [5678]
2 assert list.get(2) == 7
3 assert list[2] == 7
4 assert list instanceof java.util.List
5  
6 def emptyList = []
7 assert emptyList.size() == 0
8 emptyList.add(5)
9 assert emptyList.size() == 1

每一個列表表達式都是建立了 java.util.List的一個實例。java

Ranges

Ranges允許你建立一個值序列,這個只序列能夠被用做Lists,由於Range繼承自Java.util.List.api

Ranges 使用 ..來定義,而且包含兩端的值ide

Ranges 也可使用 ..< 來定義半開閉序列,這樣建立的序列將包含最左邊的,可是不包含最右邊的:ui

1 // an inclusive range
2 def range = 5..8
3 assert range.size() == 4
4 assert range.get(2) == 7
5 assert range[2] == 7
6 assert range instanceof java.util.List
7 assert range.contains(5)
8 assert range.contains(8)
9  
10 // lets use a half-open range
11 range = 5..<8
12 assert range.size() == 3
13 assert range.get(2) == 7
14 assert range[2] == 7
15 assert range instanceof java.util.List
16 assert range.contains(5)
17 assert ! range.contains(8)
18  
19 //get the end points of the range without using indexes
20 def range = 1..10
21 assert range.from == 1
22 assert range.to == 10

Ranges適用於任何的實現了 java.lang.Comparable接口的Java對象,並且他也有next()和 previous() 方法來訪問下一個和上一個元素。spa

好比你能夠在Ranges中是用String:rest

1 // an inclusive range
2 def range = 'a'..'d'
3 assert range.size() == 4
4 assert range.get(2) == 'c'
5 assert range[2] == 'c'
6 assert range instanceof java.util.List
7 assert range.contains('a')
8 assert range.contains('d')
9 assert ! range.contains('e')

Ranges能夠和for循環結合起來講使用:code

1 for (i in 1..10) {
2  println "Hello ${i}"
3 }

使用下面的代碼也能夠達到上述的效果:orm

1 (1..10).each { i ->
2  println "Hello ${i}"
3 }

Ranges固然也能夠用在switch中:htm

1 switch (years) {
2  case 1..10: interestRate = 0.076break;
3  case 11..25: interestRate = 0.052break;
4  default: interestRate = 0.037;
5 }

Maps

注意 [:] 實際上是一個空的Map。

Map的key若是是string的話,默認是這種形式: [a:1],它等價於["a":1]. 可是若是你真的想讓一個變量做爲key的話,那麼你必須使用括號將他包起來: [(a):1].

1 def map = [name:"Gromit", likes:"cheese", id:1234]
2 assert map.get("name") == "Gromit"
3 assert map.get("id") == 1234
4 assert map["name"] == "Gromit"
5 assert map['id'] == 1234
6 assert map instanceof java.util.Map
7  
8 def emptyMap = [:]
9 assert emptyMap.size() == 0
10 emptyMap.put("foo"5)
11 assert emptyMap.size() == 1
12 assert emptyMap.get("foo") == 5

Maps也有點想beans,這樣就可使用.號來獲取屬性:

1 def map = [name:"Gromit", likes:"cheese", id:1234]
2 assert map.name == "Gromit"
3 assert map.id == 1234
4  
5 def emptyMap = [:]
6 assert emptyMap.size() == 0
7 emptyMap.foo = 5
8 assert emptyMap.size() == 1
9 assert emptyMap.foo == 5

更有效的使用’*.’操做符

咱們可使用這個操做符來操做集合中的全部元素:

1 assert [135] == ['a''few''words']*.size()

加強的集合方法:

好比下面的例子:

1 def words = ['ant''buffalo''cat''dinosaur']
2 assert words.findAll{ w -> w.size() > 4 } == ['buffalo''dinosaur']

下面的這個例子獲取了全部元素的首字母:

1 def words = ['ant''buffalo''cat''dinosaur']
2 assert words.collect{ it[0] } == ['a''b''c''d']

切片操做符

1 def text = "nice cheese gromit!"
2 def x = text[2]
3  
4 assert x == "c"
5 assert x.class == String
6  
7 def sub = text[5..10]
8 assert sub == 'cheese'
9  
10 def map = [name:"Gromit", likes:"cheese", id:1234]
11  
12 assert map["name"] == "Gromit"
13 assert map.name == "Gromit"
14  
15 def list = [101112]
16 def answer = list[2]
17 assert answer == 12

下面再給出一些例子:

1 def list = 100..200
2 def sub = list[1320..2533]
3 assert sub == [101103120121122123124125133]

也可使用切片操做符來更新元素

1 def list = ["a""b""c"]
2 list[2] = "d"
3 list[0] = list[1]
4 list[3] = 5
5 assert list == ["b""b""d"5]

咱們可使用負數來從尾部獲取 List, array, String 等的值:

1 def text = "nice cheese gromit!"
2 def x = text[-1]
3 assert x == "!"
4  
5 def name = text[-7..-2]
6 assert name == "gromit"

若是你使用一個向後的索引,也就是說前面的值比後面的大,好比[3:1],那麼獲得的結果是相反的:

1 def text = "nice cheese gromit!"
2 def name = text[3..1]
3 assert name == "eci"

Dynamic objects (Expandos)

Expando 在嚴格意義上來講並非一個集合。可是有點相似於Map, .他允許你充分理由Groovy’s closure mechanisms來建立動態對象, 可是 Expando和Map不一樣的是他能夠提供 synthetic methods 。

1 def player = new Expando()
2 player.name = "Dierk"
3 player.greeting = { "Hello, my name is $name" }
4  
5 println player.greeting()
6 player.name = "Jochen"
7 println player.greeting()

The player.greeting assignment passes in a closure to execute when greeting() is called on the Expando. Notice that the closure has access to the properties assigned to the Expando, even though these values may change over time, using Groovy’s GString 」$variableOrProperty」 notation.

相關文章
相關標籤/搜索