Groovy using the list collection

Source: Internet
Author: User

  1. Gets the elements in the list collection

    def LST = [1,3,4,1,8,9,2,6]println lst[-1]println lst[-2] Output: output: 62
  2. Use Range object to get several consecutive values in a collection

    Take value from left to right
    def LST = [1,3,4,1,8,9,2,6]println lst[2..5] Output Result: [4, 1, 8, 9]

    Value from right to left
    def LST = [1,3,4,1,8,9,2,6]println lst[-1..-4]
    Output results: [6, 2, 9, 8]
  3. Iterative ArrayList

    Iterate from left to right in LST = [1,3,4,1,8,9,2,6]lst.each{print "${it},"} Output: 1,3,4,1,8,9,2,6,//iterates from right to left in the reverse direction of LST = [1,3,4,1,8,9,2,6] lst.each{print "${it},"} Output: 6,2,9,8,1,4,3,1,//Iteration Display index def LST = [1,3,4,1,8,9,2,6]lst.eachwithindex{it,i, print ( "${i},")} Output Result: 0,1,2,3,4,5,6,7,
  4. Using the Collect method of list

    Finding the list element/*find () finds the first occurrence of the match object, and it iterates only until the closure returns true. The resulting True,find method stops the iteration and returns the current element. Returns null if the traversal ends and does not get true. */lst = [1,3,4,1,8,9,2,6]println lst.find{it > 4} output Result: 8//Find list element, return all eligible elements LST = [1,3,4,1,8,9,2,6]println lst.findall{it > 4} output result: [8,9,6]//Find list element, return element subscript lst = [1,3,4,1,8,9,2,6]println lst.findallindexof{it = = 4} output Result: 2
  5. Sort by using list

    def ids = [5,6,3,7,1,4,9]  //Can be considered Comparator sort  ids.sort {a,b->      return-a.compareto (b)  }  println IDs  //Natural sort  ids.sort ();  println IDs output: [9, 7, 6, 5, 4, 3, 1][1, 3, 4, 5, 6, 7, 9]
  6. List to go heavy

    LST = [1,3,1,1,8,9,2,6]println lst.unique () output result: [1, 3, 8, 9, 2, 6]
  7. Link a list element to a string

    LST = [' Shun Fenghai ', ' is good ', ' only sell genuine ']println lst.join (') println lst.join (', ') output: Shun Fenghai is good only sell genuine shun Fenghai, is good, only sell authentic
  8. Element substitution

    LST = [' Shun Fenghai ', ' is good ', ' sell only authentic ']lst[0] = [' sfht ', '. com ']println lst.flatten () output: [Sfht,. com, that's good, sell only authentic]
  9. +/-Operator


    LST = [' Shun Fenghai ', ' is good ', ' only sell genuine ']println LST-[' is good '] output: [Shun Fenghai, only sell genuine]lst = [' Shun Fenghai ', ' is good ', ' sell genuine ']println lst + [' You say it '] output: [Shun Fung Sea Amoy, is good, only sells the genuine, you say?
  10. List element Leveling

    LST = [[up], ' Shun Fenghai ', ' is good ', ' only sell authentic ']println lst.flatten () output: [1, 2, ' Shun Fenghai ', ' is good ', ' only sell genuine ']

Groovy using the list collection

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.