微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Groovy笔记(3)_集合

集合概述

 

1、集合应用很广泛

2、Groovy直接在语言内使用集合

  • 不需要导入专门的类,也不需要初始化对象
  • 集合石语言本身的本地成员

3、每个Groovy集合都是java.util.Colleciton或java.util.Map的实例

4、List,String,StringBuffer,Range,Map,File,Matcher都是用同一的size()方法获取长度.

如:StringBUffer x = new StringBuffer('ssss');

      print x.size() //输出4

 

 

列表List一

 

def toys =[['a','001'],[2,'002'],['c','003']]

  • println toys.class           //输出class java.util.ArrayList
  • assert toys instanceof Collection
  • print toys[1]                   //输出[2,'002']
  • print toys.get(1)             //同上
  • print toys[-2]                  //同上
  • print toys[1..<2]             //同上
  • toys[2] = [3,'003']          //修改第3个元素
  • print toys[-1]                  //输出: [3,'003']
  • toys.putAt(2,[33,'333'])
  • print toys[-1]                  //输出[33,'333']

列表List二

 

1、toys<<[4,'004'] //追加元素

     println toys[-1] //输出:[4,'004']

 

2、toys1 = [1,2,3] //连接链表

     toys2 = toys1 + [4,5]

     println toys2  // 输出:[1,3,4,5]

 

3、toys3 = toys2 - [5]//列表中删除元素

     println toys3 //输出:[1,4]

 

列表List三

 

def toy6 =[]

  • toy6<<'11'
  • toy6<<'22'
  • toy6<<'33'
  • toy6<<'44'
  • println toy6 //输出['11','22','33','44']
  • toy6<<[8,'008']
  • println toy6 //输出['11','44',[8,'008']]  列表中元素无需一样类型    

列表List的方法

 

1、def list=[1,4]

  • list.add(5)                          //[1,5]
  • list.add(2,11)                     //[1,11,5]
  • list.addAll([6,7])                 //[1,5,6,7]
  • println list.contains(11)      //true
  • println list.containsAll([11,4])//true
  • println list.indexOf(11)       //2
  • list.remove(2);println list    //[1,7]
  • list.remveAll([5,7])
  • println list                           //[1,4]
  • list.cleat();println list           //[]

 

列表List的方法

 

1、有些方法修改原列表,有些方法修改原列表而只是产生新的列表。

     def fList =[1,3[4,5]]

  • println fList.flatten()                  //[1,5]展开后返回新列表
  • println fList.inersect([3,5])     //[3],求交集,返回新列表
  • println fList.pop()                      //[4,5] 删除列表最后元素
  • println fList.reverse()                //[3,1],反转列表返回新列表
  • println fLIst.sort()                      //[1,3]

2、计算元素

     def gList = [1,1,3]

     println gList.count(3)                      //输出:2   有两个3

 

 

映射Map一

 

1、def bookMap = [:] //定义空map

     println bookMap.getClass() // 输出class java.util.LinkedHashMap

     assert bookMap.size() == 0

 

2、def toyMap = [1:'toy1',2:'toy2']

     assert toyMap.containsValue('toy1')

     assert toyMap.containsKey(1)

 

 

映射Map二

 

1、println toyMap                        //输出整个map,[1:'toy1',2:'toy2']

     println toyMap[2]                   // toy2

     println toyMap.get(1)             // toy1

 

2、toyMap.each{toy -> println toy.key + ':' + toy.value} // 1:toy1 2:toy2(换行输出) ,toy是指定的闭包参数

     toyMao.each{println it.key + ':' + it.value}                 //输出同上,it是认闭包参数

 

 

映射MAP三

 

  • toyMap.put(3,'toy3')                        //往map中加入元素
  • println toyMap                                 //[1:'toy1',2:'toy2',3:'toy3']
  • topMap.put(3,'toy333')                    //键已存在,put就变成了修改
  • println toyMap                                 //[1:'toy1',3:'toy333']
  • toyMap.remove(3)                           //删除map中的元素,参数是键
  • println toyMap.size()                        //获取Map大小 2
  • println toMap.keySet()                      //获取Map中的key,输出[1,2]
  • println toMap.values()                      //输出:['toy1','toy2']
  • println toMap.values().asList()          //转换为ArrayList
  • println toMap.values().asList().class //class java.util.ArrayList

 

范围一

 

1、def arange = 1..<5

     println arange                //输出[1,4]

     println arange.class        //class groovy.lange.IntRange

     assert arange instanceof List

     

(1..<5)范围是IntRange的对象,是特殊的List

 

 

范围二

 

1、def bRange = 'a'..<'e'

     println bRange     // 输出: ["a","b","c","d"]

     println bRange.class   //输出groovy.lang.ObjectRange

     assert bRange instanceof List

 

('a'..<'e')是 ObjectRange的对象,是特殊的List

 

 

范围三

 

倒序

1、def cRange = 5..1

     println cRange                       //[5,1]

2、def dRange = 'e'..'a'              //["e","d","b"]

 

 

范围Range的方法

 

def eRange =1..10

  • println eRange.size()          //10
  • println eRange.contains(5) //true
  • println eRange.get(8)         //9
  • println eRange.getFrom()   //1
  • println eRange.getTo()       //10
  • prinltn eRange.isReverse() //false
  • prinltn eRange.subList(3,6)//[4,6]

原文地址:https://www.jb51.cc/groovy/842720.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐