Groovy - Set Data Structure Shows Unexpected Effects?

Hello,

I’ve been trying for the past 30 minutes to make a set make work in Groovy, but unexpected effects seem to happen. Refer to pictures provided below :
grafik

Output:

So first of all, the Set - Data Structure doesn’t seem to automatically ignore duplicates, and secondly, even with considering that the Set doesn’t ignore duplicates, the output obviously doesn’t reflect that.

Is the way I use the Groovy - API wrong or is this an issue with Groovy itself ?

PS: please provide examples as text and not image so that it’s easier to copy paste to try them :wink: thx

The following works for me:

{{groovy}}
def hello = [] as Set
hello.add("1")
hello.add("1")
println hello
{{/groovy}}

Okay, I seem to have found the issue, or one of the causes, rather.

It seems that its the context in which I execute this snippet is the issue.



Note that “emptySet” and then the println (commented out) is the relevant part. It seems that if used in this context, it leads to these effects. In a seperate Groovy Script it all works fine…

Note that def emptySet = [] is not defining a set in groovy, you need def empySet = [] as Set

Side note that you can use xwiki.getURLContent("url here") to get the content from a URL.