Javatpoint Logo
Javatpoint Logo

Set in Java

The set is an interface available in the java.util package. The set interface extends the Collection interface. An unordered collection or list in which duplicates are not allowed is referred to as a collection interface. The set interface is used to create the mathematical set. The set interface use collection interface's methods to avoid the insertion of the same elements. SortedSet and NavigableSet are two interfaces that extend the set implementation.

Set in Java

In the above diagram, the NavigableSet and SortedSet are both the interfaces. The NavigableSet extends the SortedSet, so it will not retain the insertion order and store the data in a sorted way.

SetExample1.java

Output:

Set in Java

Note: Throughout the section, we have compiled the program with file name and run the program with class name. Because the file name and the class name are different.

Operations on the Set Interface

On the Set, we can perform all the basic mathematical operations like intersection, union and difference.

Suppose, we have two sets, i.e., set1 = [22, 45, 33, 66, 55, 34, 77] and set2 = [33, 2, 83, 45, 3, 12, 55]. We can perform the following operation on the Set:

  • Intersection: The intersection operation returns all those elements which are present in both the set. The intersection of set1 and set2 will be [33, 45, 55].
  • Union: The union operation returns all the elements of set1 and set2 in a single set, and that set can either be set1 or set2. The union of set1 and set2 will be [2, 3, 12, 22, 33, 34, 45, 55, 66, 77, 83].
  • Difference: The difference operation deletes the values from the set which are present in another set. The difference of the set1 and set2 will be [66, 34, 22, 77].

In set, addAll() method is used to perform the union, retainAll() method is used to perform the intersection and removeAll() method is used to perform difference. Let's take an example to understand how these methods are used to perform the intersection, union, and difference operations.

SetExample2.java

Output:

Set in Java

Description:

In the above code, first, we create two arrays, i.e., A and B of type integer. After that, we create two set, i.e., set1 and set2 of type integer. We convert both the array into a list and add the elements of array A into set1 and elements of array B into set2.

For performing the union, we create a new set union_data with the same element of the set1. We then call the addAll() method of set and pass the set2 as an argument to it. This method will add all those elements to the union_data which are not present in it and gives the union of both sets.

For performing the intersection, we create a new set intersection_data with the same element of the set1. We then call the retainAll() method of set and pass the set2 as an argument to it. This method will get all those elements from the intersection_data which are present in set2 and store it in the intersection_data. Now, the intersection_data contains the intersect value of both the sets.

For performing the difference, we create a new set difference_data with the same element of the set1. We then call the removeAll() method of set and pass the set2 as an argument to it. This method will remove all those elements from the difference_data which are present in the set2 and gives the difference of both the sets.

Set Methods

There are several methods available in the set interface which we can use to perform a certain operation on our sets. These methods are as follows:

1) add()

The add() method insert a new value to the set. The method returns true and false depending on the presence of the insertion element. It returns false if the element is already present in the set and returns true if it is not present in the set.

Syntax:

SetExample3.java

Output:

Set in Java

2) addAll()

The addAll() method appends all the elements of the specified collection to the set.

Syntax:

SetExample4.java

Output:

Set in Java

3) clear()

The method removes all the elements from the set. It doesn't delete the reference of the set. It only deletes the elements of the set.

Syntax:

SetExample5.java

Output:

Set in Java

4) contains()

The contains() method is used to know the presence of an element in the set. Its return value is true or false depending on the presence of the element.

Syntax:

SetExample6.java

Output:

Set in Java

5) containsAll()

The method is used to check whether all the elements of the collection are available in the existing set or not. It returns true if all the elements of the collection are present in the set and returns false even if one of the elements is missing in the existing set.

Syntax:

SetExample7.java

Output:

Set in Java

6) hashCode()

The method is used to derive the hash code value for the current instance of the set. It returns hash code value of integer type.

Syntax:

SetExample8.java

Output:

Set in Java

7) isEmpty()

The isEmpty() method is used to identify the emptiness of the set . It returns true if the set is empty and returns false if the set is not empty.

Syntax:

SetExample9.java

Output:

Set in Java

8) iterator()

The iterator() method is used to find the iterator of the set. The iterator is used to get the element one by one.

Syntax:

SetExample10.java

Output:

Set in Java

9) remove()

The method is used to remove a specified element from the Set. Its return value depends on the availability of the element. It returns true if the element is available in the set and returns false if it is unavailable in the set.

Syntax:

SetExample11.java

Output:

Set in Java

11) removeAll()

The method removes all the elements of the existing set from the specified collection.

Syntax:

SetExample12.java

Output:

Set in Java

11) retainAll()

The method retains all the elements from the set specified in the given collection.

Syntax:

SetExample13.java

Output:

Set in Java

12) size()

The method returns the size of the set.

Syntax:

SetExample14.java

Output:

Set in Java

13) removeAll()

The method is used to create an array with the same elements of the set.

Syntax:

SetExample15.java

Output:

Set in Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA