The collection in groovy is also much simpler to use than Java, and it's nice to note that groovy also offers many handy ways to sort the numbers in a collection, to find the maximum or minimum value, to reverse the elements, to overlap, to remove duplicate elements, to merge the arrays, to select the features that fit the criteria, Because these methods intellij the IDE with code hints, they are not explained.
The most common operation of the 1.list collection
Package org.lxh//Collection Simple use class UseArray01 {//Collection add element operation static main (args) {//list set operation def list=[]//Add element list << "Hello" List.add ("World") println list println list[1]}}
Groovy provides the << operator to put elements into the list, while preserving the Add method, you can use one of them according to their own habits, traversal will not say that with each and loop can be implemented
2. A number of complex list operations
Package org.lxhclass usearray02 {static main (args) { //join method Def array=[1989,12,25] println array.join ("-") println array[1,2].join ("- ") //list subtraction println array-[25] //Remove duplicate elements def arr1=[ 1,2,3,4,4,5,6,6,7] print arr1.unique () //intersection def arr2=["Beijing", "Shanghai" def arr3=["Shanghai", "Ningbo"] println arr2.intersect (ARR3) //set reversal println arr3.reverse () //list sort def arr4=[8,6,5,1,9,3] Println arr4.sort () //list scrambled Element order println arr4.sort{ math.random () } //gets the index of the corresponding element println arr1.indexof (5) //list sum Println arr4.sum () //remove maximum and minimum println arr4.max () //element Filtering def arr5=[3,8,9,10,5,6] println arr5.grep{ it>6 } }}
Groovy list is not very powerful, the article just lists some of the more basic usage, there are some less commonly used to check the information online.
Use of the list collection in groovy