ZetCode

Groovy Sets

last modified October 15, 2026

A set is a collection that holds each element at most once. Groovy works with the Java Set interface and adds convenient syntax for creating sets and performing set operations. This tutorial covers creating sets, adding and removing elements, and the common union, intersection, and difference operations.

Creating Sets

Use the as Set conversion on a list, or call toSet() on a collection. Duplicates are dropped automatically. Groovy also accepts the Set constructors from Java.

CreateSet.groovy
def tags = ["groovy", "java", "groovy", "kotlin"] as Set
println tags
println tags.size()

def numbers = [1, 1, 2, 3, 3].toSet()
println numbers

Both forms remove the duplicate "groovy" and 3. The result is a HashSet, so the iteration order is not guaranteed.

Adding, Removing, and Checking Elements

The add method returns true only when the element was not already present. remove works the same way in reverse. Use contains for membership tests.

AddRemoveSet.groovy
def tags = ["groovy", "java"] as Set

println tags.add("java")
println tags.add("scala")
println tags.remove("groovy")
println tags.contains("scala")
println tags

Adding "java" again returns false because it is already in the set. This return value is useful when you need to detect duplicates while processing data.

Set Operations

The + and - operators give union and difference, and intersect returns the common elements. Each operation returns a new collection and leaves the originals unchanged.

SetOperations.groovy
def a = [1, 2, 3, 4] as Set
def b = [3, 4, 5] as Set

println a + b
println a - b
println a.intersect(b)
println a.containsAll([1, 2])

a + b contains every value from both sets, a - b keeps the values only in a, and intersect keeps the values in both. containsAll checks whether one collection holds every element of another.

Filtering a Set

Closure methods such as findAll and collect work on sets. Filtering a set returns a new collection, so the original set is unchanged.

FilterSet.groovy
def scores = [45, 72, 88, 72, 91] as Set

def passing = scores.findAll { it >= 60 }
println passing
println scores.collect { it / 10 as int }
println scores.size()

Because 72 appears twice in the input, the set holds it once. The closure methods then work on the unique values only.

Choosing a Set Implementation

as Set creates a HashSet, which is fast but unordered. Use LinkedHashSet to keep insertion order, or TreeSet to keep elements sorted.

SetImplementations.groovy
def ordered = new LinkedHashSet(["c", "a", "b", "a"])
println ordered

def sorted = new TreeSet([5, 1, 3, 1])
println sorted

The LinkedHashSet prints [c, a, b], the order of first insertion. The TreeSet prints [1, 3, 5], sorted with duplicates removed.

Best Practices

Source

Groovy Development Kit | Java Set API

Sets give you unique collections with fast membership tests and convenient operators for combining them. Choosing the right implementation keeps your code both correct and easy to read.

Author

My name is Jan Bodnar, and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Groovy tutorials.