> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/set/objecttoset.md).

# ObjectToSet

Convert a collection into a Set, deduplicating automatically.

Accepts an Array, a list-delimited String, an existing Set, a QueryColumn, an XML node, a bounded Range, or any value castable to a Set. When the value is already of the requested variant it is returned as-is; otherwise a new Set of the specified variant is created and populated.

## Method Signature

```
ObjectToSet(value=[any], type=[string], delimiter=[string])
```

### Arguments

| Argument    | Type     | Required | Description                                                                                                                                                                                                            | Default |
| ----------- | -------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `value`     | `any`    | `true`   | The value to convert. Accepts Array, Set, list-delimited String, QueryColumn, XML, Range, etc.                                                                                                                         |         |
| `type`      | `string` | `false`  | <p>The backing variant: "default" / "hash" (HashSet), "linked" / "ordered" (LinkedHashSet),<br>or "sorted" / "tree" (TreeSet). When omitted, the caster defaults to LINKED for ordered<br>collections like Arrays.</p> |         |
| `delimiter` | `string` | `false`  | When {@code value} is a String, the list delimiter to split on. Defaults to {@code ","}.                                                                                                                               | `,`     |

## Examples

### Convert an Array to a Set

Calling `.toSet()` on an Array deduplicates its elements and returns a BoxSet.

```java
s = [ 1, 2, 2, 3 ].toSet();
writeOutput( s.size() );

```

Result: 3

### Convert an Array to a linked (insertion-ordered) Set

Pass a type argument to control the backing implementation.

```java
s = [ "c", "a", "b", "a" ].toSet( "linked" );
writeOutput( s.toArray().toString() );

```

Result: \[c, a, b]

### Convert an Array to a sorted Set

```java
s = [ 9, 1, 5, 3 ].toSet( "sorted" );
writeOutput( s.toArray().toString() );

```

Result: \[1, 3, 5, 9]

### Split a comma-delimited string into a Set

`listToSet()` splits the string on the default delimiter and deduplicates.

```java
s = "a,b,c,a".listToSet();
writeOutput( s.size() );

```

Result: 3

### Split a string with a custom delimiter

```java
s = "a|b|c|b".listToSet( delimiter="|" );
writeOutput( s.size() );

```

Result: 3

### Split with a custom delimiter and preserve insertion order

```java
s = "a|b|c|b".listToSet( delimiter="|", type="linked" );
writeOutput( s.toArray().toString() );

```

Result: \[a, b, c]

### Convert a Query to a Set of row structs

Each row becomes a Struct element in the Set.

```java
q = queryNew( "name,age", "varchar,integer", [ [ "Alice", 30 ], [ "Bob", 25 ] ] );
s = q.toSet();
writeOutput( s.size() );

```

Result: 2

### Get distinct column values as a Set

Use `.columnData()` to extract a column as an Array first, then call `.toSet()` to deduplicate.

```java
q = queryNew( "name", "varchar", [ [ "Alice" ], [ "Bob" ], [ "Alice" ] ] );
s = q.columnData( "name" ).toSet();
writeOutput( s.size() );

```

Result: 2

## Related

* [BoxSetAdd](/boxlang-language/reference/built-in-functions/set/boxsetadd.md)
* [BoxSetAddAll](/boxlang-language/reference/built-in-functions/set/boxsetaddall.md)
* [BoxSetClear](/boxlang-language/reference/built-in-functions/set/boxsetclear.md)
* [BoxSetContains](/boxlang-language/reference/built-in-functions/set/boxsetcontains.md)
* [BoxSetContainsAll](/boxlang-language/reference/built-in-functions/set/boxsetcontainsall.md)
* [BoxSetDifference](/boxlang-language/reference/built-in-functions/set/boxsetdifference.md)
* [BoxSetEach](/boxlang-language/reference/built-in-functions/set/boxseteach.md)
* [BoxSetEquals](/boxlang-language/reference/built-in-functions/set/boxsetequals.md)
* [BoxSetEvery](/boxlang-language/reference/built-in-functions/set/boxsetevery.md)
* [BoxSetFilter](/boxlang-language/reference/built-in-functions/set/boxsetfilter.md)
* [BoxSetFind](/boxlang-language/reference/built-in-functions/set/boxsetfind.md)
* [BoxSetIntersection](/boxlang-language/reference/built-in-functions/set/boxsetintersection.md)
* [BoxSetIsDisjointFrom](/boxlang-language/reference/built-in-functions/set/boxsetisdisjointfrom.md)
* [BoxSetIsEmpty](/boxlang-language/reference/built-in-functions/set/boxsetisempty.md)
* [BoxSetIsSubsetOf](/boxlang-language/reference/built-in-functions/set/boxsetissubsetof.md)
* [BoxSetIsSupersetOf](/boxlang-language/reference/built-in-functions/set/boxsetissupersetof.md)
* [BoxSetMap](/boxlang-language/reference/built-in-functions/set/boxsetmap.md)
* [BoxSetNone](/boxlang-language/reference/built-in-functions/set/boxsetnone.md)
* [BoxSetReduce](/boxlang-language/reference/built-in-functions/set/boxsetreduce.md)
* [BoxSetReject](/boxlang-language/reference/built-in-functions/set/boxsetreject.md)
* [BoxSetRemove](/boxlang-language/reference/built-in-functions/set/boxsetremove.md)
* [BoxSetRemoveAll](/boxlang-language/reference/built-in-functions/set/boxsetremoveall.md)
* [BoxSetRetainAll](/boxlang-language/reference/built-in-functions/set/boxsetretainall.md)
* [BoxSetSome](/boxlang-language/reference/built-in-functions/set/boxsetsome.md)
* [BoxSetSymmetricDifference](/boxlang-language/reference/built-in-functions/set/boxsetsymmetricdifference.md)
* [BoxSetToArray](/boxlang-language/reference/built-in-functions/set/boxsettoarray.md)
* [BoxSetToList](/boxlang-language/reference/built-in-functions/set/boxsettolist.md)
* [BoxSetUnion](/boxlang-language/reference/built-in-functions/set/boxsetunion.md)
* [SetNew](/boxlang-language/reference/built-in-functions/set/setnew.md)
* [SetOf](/boxlang-language/reference/built-in-functions/set/setof.md)
* [StructKeySet](/boxlang-language/reference/built-in-functions/set/structkeyset.md)
* [StructValueSet](/boxlang-language/reference/built-in-functions/set/structvalueset.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/set/objecttoset.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
