Reputation: 2165
I'm using soapui groovy script. I want to remove the duplicate from a list, using the next code:
def myStr = "aaaa ; bbbb ; ccccc"
def myList = myStr.split(";")
myList = myList.unique()
but when i tun the code i get exception:
No signature of method: [Ljava.lang.String;.unique() is applicable for argument types: () values: [] Possible solutions: minus(java.lang.Object), minus(java.lang.Iterable), minus([Ljava.lang.Object;), size(), use([Ljava.lang.Object;), use(java.lang.Class, groovy.lang.Closure)
Upvotes: 2
Views: 20099
Reputation: 25864
Simple, .split() returns an array, you just need to convert it to a (Groovy) List. Any of the following will make the unique() method work.
def myList = myStr.split(";").collect()
or
def myList = (List)myStr.split(";")
or
def myList = myStr.split(";").toList()
If you cast it to a java.util.Set, it'll only keep unique values!
def myList = (Set)myStr.split(";")
Gotcha: Be careful though, the strings still contain the spaces!
Upvotes: 1
Reputation: 50245
Use tokenize()
instead of split()
which returns an ArrayList as compared to split which return a String Array.
def myStr = "aaaa ; bbbb ; ccccc;"
def myList = myStr.tokenize(";")*.trim()
myList = myList.unique()
or use toList()
if you are using split()
or cast the String array to a Set
.
However, based on the question you want to remove the duplicate items from list but I do not see any duplicate item. If you mean to remove duplicate strings from the list items then use:
myList = myList.unique().collect { it.toSet().join() }
Upvotes: 5