# Groovy - Set Data Structure Shows Unexpected Effects?

**URL:** <https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364>\
**Category:** Help / Discuss\
**Tags:** groovy\
**Created:** [August 19, 2024, 12:51pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364 "2024-08-19T12:51:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elias](https://forum.xwiki.org/letter_avatar_proxy/v4/letter/e/41988e/32.png) [@Elias](https://forum.xwiki.org/u/Elias)\
**Post date:** [August 19, 2024, 12:51pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/1 "2024-08-19T12:51:15Z")

</div>

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](https://forum.xwiki.org/uploads/default/original/2X/3/3e8798b1d651fa2f5a28711b51be8985b1f670fc.png)

Output:

 ![grafik](https://forum.xwiki.org/uploads/default/original/2X/4/40a464a7edb7edd8a6cfd28fd3c40f0ecaf07244.png)

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 ?

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [August 19, 2024, 1:17pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/2 "2024-08-19T13:17:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [August 19, 2024, 1:20pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/3 "2024-08-19T13:20:51Z")

</div>

The following works for me:

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

```

---

<div class="post-metadata">

**Author:** ![Elias](https://forum.xwiki.org/letter_avatar_proxy/v4/letter/e/41988e/32.png) [@Elias](https://forum.xwiki.org/u/Elias)\
**Post date:** [August 19, 2024, 2:52pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/4 "2024-08-19T14:52:20Z")

</div>

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.

 ![grafik](https://forum.xwiki.org/uploads/default/original/2X/6/6c8bd07a2bbbdbb91585042d45fe176871721f0a.png)  
 ![grafik](https://forum.xwiki.org/uploads/default/original/2X/a/a9697faeaf245f67fae78cb12e0b321d24f88646.png)  
 ![grafik](https://forum.xwiki.org/uploads/default/original/2X/d/db804176a7886279913d4a9da765d09e63250612.png)

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…

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [August 19, 2024, 3:28pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/5 "2024-08-19T15:28:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [August 19, 2024, 3:29pm UTC](https://forum.xwiki.org/t/groovy-set-data-structure-shows-unexpected-effects/15364/6 "2024-08-19T15:29:34Z")

</div>

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