First Groovy

 1 class Sample {
 2     def names = ["anna", "annie", "tommy", "bobby", "doggy"];
 3 
 4     public static void main(def args) {
 5         //Loop
 6         for (i in 0..5) {
 7             println "Hello, World!"
 8         }
 9 
10         //Closure
11         def sample = new Sample();
12         //Operation of array
13         sample.names << "Terry";
14         println sample.names;
15 
16         println "-------"
17         //lambda expression   each
18         sample.names.each({ e -> println(e) });
19 
20         println "-------"
21         //keyword it.
22         sample.names.each { println it }
23 
24         println "-------"
25 
26         //filter
27         List<String> matched = sample.names.findAll { e -> e.contains("o") }
28         matched.each { println it }
29 
30         println "-------"
31         //sort
32         sample.names << "William";
33         sample.names << "bob";
34         List<String> sorted = sample.names.sort({ e -> e.length() })
35         sorted.each { println it }
36 
37         println "-------"
38         //grouped
39         Map<Boolean, String> grouped = sample.names.groupBy { e -> e.contains("o") }
40         grouped.each { key, value -> println "$key:$value" }
41     }
42 }

 

posted @ 2013-10-25 16:29  史蒂芬.王  阅读(230)  评论(0编辑  收藏  举报