> For the complete documentation index, see [llms.txt](https://gyansetu-core-java-for-java.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-core-java-for-java.gitbook.io/project/collections-framework-part-2/sets.md).

# Sets

## Set in Java

* Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.
* Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
* Set has various methods to add, remove clear, size, etc to enhance the usage of this interface

```java
// Java code for adding elements in Set 
import java.util.HashSet;
import java.util.Set; 
public class SetExample 
{ 
	public static void main(String[] args) 
	{ 
		// Set demo using HashSet 
		Set<String> hash_Set = new HashSet<String>(); 
		
		hash_Set.add("GyanSetu"); 
		hash_Set.add("For"); 
		hash_Set.add("GyanSetu"); 
		hash_Set.add("Example"); 
		hash_Set.add("Set"); 
		System.out.print("Set output without the duplicates"); 

		System.out.println(hash_Set); 
 
	} 
} 
```

```
Set output without the duplicates[GyanSetu, Set, Example, For]
```

### Basic Set operation :

**Union**\
In this, we could simply add one Set with other. Since the Set will itself not allow any duplicate entries, we need not take care of the common values.

**Expected Output:**

```
Union : [0, 1, 2, 3, 4, 5, 7, 8, 9]
```

&#x20;\
**Intersection**\
We just need to retain the common values from both Sets.

**Expected Output:**

```
Intersection : [0, 1, 3, 4]
```

&#x20;\
**Difference**\
We just need to remove all the values of one Set from the other.\
\
**Expected Output:**

```
Difference : [2, 8, 9]
```

```java
// Java code for demonstrating union, intersection and difference 
// on Set 
import java.util.HashSet;
import java.util.Set; 
public class SetExample 
{ 
	public static void main(String args[]) 
	{ 
		Set<Integer> a = new HashSet<Integer>(); 
		a.add(1);
		a.add(3);
		a.add(2);
		a.add(4);
		a.add(8);
		a.add(9);
		a.add(0);
		Set<Integer> b = new HashSet<Integer>(); 
		b.add(1);
		b.add(3);
		b.add(7);
		b.add(5);
		b.add(4);
		b.add(0);
		b.add(7);
		a.add(5);
		
		// To find union 
		Set<Integer> union = new HashSet<Integer>(a); 
		union.addAll(b); 
		System.out.print("Union of the two Set"); 
		System.out.println(union); 

		// To find intersection 
		Set<Integer> intersection = new HashSet<Integer>(a); 
		intersection.retainAll(b); 
		System.out.print("Intersection of the two Set"); 
		System.out.println(intersection); 

		// To find the symmetric difference 
		Set<Integer> difference = new HashSet<Integer>(a); 
		difference.removeAll(b); 
		System.out.print("Difference of the two Set"); 
		System.out.println(difference); 
	} 
} 
```

```
Union of the two Set[0, 1, 2, 3, 4, 5, 7, 8, 9]
Intersection of the two Set[0, 1, 3, 4, 5]
Difference of the two Set[2, 8, 9]
```

### Problems:

**1.** Write a Java program to append the specified element to the end of a hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-1.php)

**2.** Write a Java program to iterate through all elements in a hash list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-2.php)

**3.** Write a Java program to get the number of elements in a hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-3.php)

**4.** Write a Java program to empty an hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-4.php)

**5.** Write a Java program to test a hash set is empty or not. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-5.php)

**6.** Write a Java program to clone a hash set to another hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-6.php)

**7.** Write a Java program to convert a hash set to an array. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-7.php)

**8.** Write a Java program to convert a hash set to a tree set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-8.php)

**9.** Write a Java program to convert a hash set to a List/ArrayList. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-9.php)

**10.** Write a Java program to compare two hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-10.php)

**11.** Write a Java program to compare two sets and retain elements which are same on both sets. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-11.php)

**12.** Write a Java program to remove all of the elements from a hash set. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-hash-set-exercise-12.php)

### CC:

{% embed url="<https://www.hackerrank.com/challenges/java-hashset/problem>" %}
