Groovy list join

1. groovy list with primitive data type, just use "Join" like:

def numbers = [0, 1, 2, 3, 4, 5] as Integer[]
assert '012345' == numbers.join()
assert '0 x 1 x 2 x 3 x 4 x 5' == numbers.join(' x ')
assert '0 1 2 3 4 5' == numbers.join(' ')

2. groovy list with object as type, like

class CurrencyType implements Serializable{
  int id
  String code
  String currency
  String toString() {
        "id:$id, code:$code,currency:$currency"
    }

}

def currenciesList = [
     new CurrencyType(id:1,code:"INR", currency:"Indian Rupee"),
     new CurrencyType(id:1,code:"USD", currency:"US Dollar"),
     new CurrencyType(id:1,code:"CAD", currency:"Canadian Dollar")

]

println currenciesList.collect{"${it.toString()}"}.join("\n")




猜你喜欢

转载自blog.csdn.net/zmmhmily/article/details/80266535