Use of the list collection in groovy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.