# Set in Java

![Class Diagram](/files/-MlTUsFXkmzvHghN5UPZ)

**import**

```java
import java.util.Set;
import java.util.HashSet;
```

## Set

Set have its implementation in various classes like `HashSet` , `TreeSet` , `LinkedHashSet` .

```java
// Hashset Random Sorting
Set<T> set = new HashSet<T>();

// TreeSet - By compareTo() or Comparator
TreeSet<T> sortedSet = new TreeSet<T>();

// LinkedHashSet - Insertion Order
LinkedHashSet<T> linkedhashset = new LinkedHashSet<T>();
```

### Creating a set

```java
Set<Integer> set = new HashSet<Integer>();
// Creates an empty Set of Integers

Set<Integer> linkedHashSet = new LinkedHashSet<Integer>(); 
//Creates a empty Set of Integers, with predictable iteration order
```

### Adding elements to a Set

```
set.add(12); 
set.add(13);
```

### Delete all the elements of a Set

```java
set.clear();
//Removes all objects from the collection.

set.remove(0); 
// Removes first occurrence of a specified object from the collection
```

### Check size

```
set.size(); 
//Returns the number of elements in the collection

set.isEmpty();
//Returns true if the set has no elements
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev117uday.gitbook.io/notes-md/java/data-structures/set-in-java.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
