Groovy之數據結構

  • 2019 年 12 月 26 日
  • 筆記

列表list

定義

Java中定義方式:def list = ArrayList()//java 中定義列表方式

Groovy中定義方式:def list = [1, 2, 3, 4, 5]//groovy 中定義列表方式

增加

  1. 直接通過add的方式添加元素,與Java一直
  2. 使用Groovy特有的語法leftShift進行添加,例如:listMake.leftShift(9)
  3. 使用<<進行添加元素,例如:listMake << 8
def listMake = [1, 2, 3, 4]  /**   * list 添加元素   */  //listMake.add(10)  //listMake.leftShift(9)  listMake &lt;&lt; 8  println listMake

刪除

  1. 通過與Java一樣的方法remove與removeAt方法
  2. 可以自定義閉包對集合中的元素特殊處理進行移除操作
  3. 可以通過-符號進行刪除集合中存在的元素,有就刪除,沒有不刪除
/**   * list 刪除元素   */  //listMake.remove(0)  //listMake.removeAt(1)  //listMake.removeElement(2)     //自定義閉包方式移除集合中所有的奇數  listMake.removeAll { return (it &amp; 1) == 1 }  //減去當前集合中存在的元素,有就移除,沒有不移除  println listMake - [2, 4]

查找

  1. find 查找集合中第一個滿足條件的元素,並返回查找元素
  2. findAll 查找集合中所有滿足條件的元素,返回的是一個集合
  3. any 返回的是Boolean,集合中有一個元素滿足條件,就返回true,否則false
  4. every 與any一樣返回的是Boolean,不同的是集合中所有的元素都需要滿足該條件才返回true,如果有一個沒有滿足就返回false
*  列表查找   */  def findList = [2, 4, 3, 5, 7, -100]  //查找第一個滿足是奇數條件的數  int result = findList.find { return (it &amp; 1) == 1 }  println result  def resultLsit = findList.findAll { return (it &amp; 1) == 0 }  println resultLsit.toListString()     def anyResult = findList.any { return (it &amp; 1) == 1 }  println anyResult  def everyResult = findList.every { return (it &amp; 1) == 1 }  println everyResult  println findList.min()  //獲取絕對值的最大值  println findList.max { Math.abs(it) }

排序

列表排序可以自定義閉包的方式進行排序,例如:Comparator mc = { a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1 } 或者直接調用sort方法進行排序,也可以在sort方法進行自定義閉包形式排序

/**   * 列表排序   */  def sortList = [-4, -10, 23, 4, 11, 9]  //自定義排序方式,按照絕對值進行排序  Comparator mc = { a, b -&gt; a == b ? 0 : Math.abs(a) &lt; Math.abs(b) ? -1 : 1 }  //Collections.sort(sortList,mc)  //sortList.sort()  //按照絕對值從大到小進行排序  sortList.sort { a, b -&gt; a == b ? 0 : Math.abs(a) &lt; Math.abs(b) ? 1 : -1 }  println sortList

其他操作

def array = [2, 3, 4, 5] as int[]//將列錶轉換成數組形式  int[] array2 = [1, 2, 3, 4]     println findList.min()  //獲取絕對值的最大值  println findList.max { Math.abs(it) }     //統計個數  def count = findList.count { return (it &amp; 1) == 1 }  println count

Map

值得注意的是:groovy 中定義map,默認都是LinkedHashMap。但可以通過as HashMap轉換成HashMap,或者一開始就定義HashMap,否則默認都是LinkedHashMap

添加

  1. 通過 color.complex = [a: 1, b: 2]
  2. 通過leftShirt進行添加,例如:color.leftShift("ss": "222")
def color = [&quot;red&quot;  : &quot;ff0000&quot;,               &quot;green&quot;: &quot;00ff00&quot;,               &quot;black&quot;: &quot;000000&quot;]     //索引方式  println color[&#039;red&#039;]  println color.green  //添加元素,查找元素,沒有則添加  color.yello = &quot;fff000&quot;  //添加新的map  color.complex = [a: 1, b: 2]  //添加一個元素  color.leftShift(&quot;ss&quot;: &quot;222&quot;)  println color.toMapString()  //只能通過getClass 來獲取類型,否則color.class 就是查找當前中是否有key為class內容,沒有則返回null  println color.getClass()

遍歷

  1. 通過each進行遍歷
  2. 通過eachWithIndex進行索引遍歷
  3. 通過key與value進行遍歷
def student = [          1: [number: 001, score: 45, age: 23, name: &quot;ztz&quot;],          2: [number: 002, score: 56, age: 25, name: &quot;yif&quot;],          3: [number: 003, score: 67, age: 26, name: &quot;fxy&quot;],          4: [number: 004, score: 78, age: 28, name: &quot;cxm&quot;],  ]     //遍歷  student.each { def stu -&gt;      println &quot;the key is ${stu.key},and the value is ${stu.value}&quot;  }     /**   * 索引遍歷 注意:student與index不能顛倒順序,否則輸出報錯,因為會首先迭代獲取當前map中內容   */  student.eachWithIndex { def stu, int index -&gt;      println &quot;the index is ${index},&quot; + &quot;the key is ${stu.value}&quot; + &quot; the value is ${stu.value}&quot;  }     //直接遍歷 key與value  student.each { key, value -&gt;      println &quot;the key is $key,&quot; + &quot;the value is $value&quot;  }     student.eachWithIndex { key, value, index -&gt;      println &quot;the index is $index,&quot; + &quot;the key is $key,&quot; + &quot;the value is $value&quot;  }

查找

  1. find 中閉包處理,內部通過get來通過key獲取value值,返回第一個符合條件的對象
  2. findAll 中閉包處理,內部通過get來通過key獲取value值,返回所有符合條件的對象
/**   * map 查找   */  def student = [          1: [number: 001, score: 45, age: 24, name: &quot;ztz&quot;],          2: [number: 002, score: 56, age: 25, name: &quot;yif&quot;],          3: [number: 003, score: 67, age: 26, name: &quot;fxy&quot;],          4: [number: 004, score: 78, age: 26, name: &quot;cxm&quot;],  ]     def entry = student.find { def students -&gt;      //必須通過get來獲取key值      return students.value.get(&quot;score&quot;) &gt;= 60  }  def entrys = student.findAll { def stu -&gt;      return stu.value.get(&quot;score&quot;) &gt;= 60  }  println entry  println entrys     def counts = student.count { def stu -&gt;      return stu.value.get(&quot;score&quot;) &gt;= 60 &amp;&amp; stu.value.get(&quot;age&quot;) == 26  }  println counts     def names = student.findAll { def stu -&gt;      return stu.value.get(&quot;score&quot;) &gt;= 60  }.collect {      //使用collect進行過濾,只輸出名字      return it.value.get(&quot;name&quot;)  }  println names.toListString()     //groupBy 進行分組輸出  def groupResult = student.groupBy { def stu -&gt;      return stu.value.get(&quot;score&quot;) &gt;= 60 ? &quot;及格&quot; : &quot;不及格&quot;  }  println groupResult.toMapString()

排序

與列表排序類似,通過閉包進行自定義排序方式

/*  map 排序   */  def sortResult = student.sort { def stu1, def stu2 -&gt;      Number sore1 = stu1.value.get(&quot;score&quot;)      Number sore2 = stu2.value.get(&quot;score&quot;)      return sore1 == sore2 ? 0 : sore1 &gt; sore2 ? 1 : -1  }  println sortResult.toMapString()

Range

Range 返回處理,可以進行each返回輸出,或者在switch中定義範圍進行處理

/*  範圍   */  def range = 1..10  println range[0]  println range.contains(10)  //獲取範圍起始值  println range.from  println range.to  range.each {      print it + &quot; &quot;  }     def getGrade(Number number) {      def result = 0      switch (number) {          case 0..&lt;60:              result = &quot;不及格&quot;              break          case 60..&lt;70:              result = &quot;及格&quot;              break          case 70..&lt;80:              result = &quot;良好&quot;              break          case 80..&lt;100:              result = &quot;優秀&quot;              break      }      //return 可以省略      return result  }     println getGrade(90)