Java

Java Sets

As the name suggests, the sets in Java are utilized to generate the mathematical sets. The Java sets are the interface provided by the “java.util” package. Set interfaces are extensions of collection interfaces. A collection interface is an unordered collection where duplicates are not allowed. That means that there is a restriction on adding the duplicate elements in the collection interfaces. To prevent the insertion of similar elements, the set platform allows the use of collection interface methods. The set interface provides several methods that we utilize to carry out certain operations on our sets.

Example 1:

The add() method from the set interface is used to add the new elements to the set. The add() method only includes the specified element if they are not found in the given set. If the elements are present in the set, it returns false.

The “JavaSetExample1” class is created where the main() method is present. In this class’s main() method, we declare the “MySet” object of string type. And a set interface as HashSet is implemented there.

After that, we use the set interface add() method to insert the elements in the set. We add five string values by calling the add() method with the set object, “MySet”. The add() method allows us to insert each element separately in the program. Then, the added collection of the elements of the set is printed using the println() method. The sorting of the provided collection of the elements of the set is also performed in the program by deploying the TreeSet which takes the “MySet” object as a parameter to sort the given set.

The elements are added in the following set using the add() method. Furthermore, the set of elements is also sorted in the form of TreeSet.

Example 2:

The contains() method from the set interface of Java is used to determine the presence of the particular element which is added to the set. A Boolean result of true is produced when the specific members in a specified set are included; a Boolean result of false is received when they are not.

The main() method is constructed inside the Java class “JavaSetExample2”. Here, we define a string-type object “SetOfNames” and implement the HashSet class for the set interface. Next, we invoke the add() method with the “SetOfNames” object to insert the names in the specified set. After that, the set collection is printed by passing the “SetOfNames” object as a parameter in the println() method.

Then, we declare another object, “MyName”, which is initialized with the string name that is present in the given set. We use the Java set interface contains() method to check whether the given string name is present in the provided set. The results of the contains() method is shown by the println() method.

The output screen first shows the set which is the collection of specified names. Then, the true Boolean result is displayed since the string name is already present in the set.

Example 3:

The hashcode() method is also the method of the set interface. The hashcode() method is used to evaluate the hash values that are passed as an argument inside it. The integer value is returned from the hashcode() method which is the input object’s hash value.

The Java class “JavaSetExample3” is implemented with the main() method. Here, we create the “Numbers” object and define the set interface using the HashSet class for integers. The set of numbers is expanded using the add() method. Then, these numerical values are printed which forms the set. We also print the hash codes of the “Numbers” set by employing the hashCode() method.

The set is displayed on the screen which contains all the numerical values. The hash code value of the set is also shown on the screen.

Example 4:

The set interface provides another method: the remove() and removeAll() methods. The specific element can be eliminated from the set using the remove() method. On the other hand, the removeAll() method removes the entire collection of elements from the given set.

The Java class “JavaSetExample4” is established where the main() method is deployed. We declare the “D1” object where the set interface of a “LinkedHashSet” class is defined. Then, we add the integer data to the set and display it. From the given collection of data inside the set, we remove the integer data “2003”. This element can be eliminated from the collection’s specified set using the remove() method.

Next, we define again the set interface for strings in the “D2” object. The set interface is created from the ArrayList class. We insert three strings in the set with the add() method. The set of strings are displayed on the terminal. After that, we call the removeAll() method which takes the input “D2” object as a parameter. The removeAll() removes all the data inside the string set. We print the set after removing all the elements at once.

Note that the output console first shows the integer set and then displays the set after the remove() method operation. Also, the results of the removeAll() method are shown on the screen for the string set. The empty set is shown since all the elements are removed by the removeAll() method.

Example 5:

Mathematical operations like union, intersection, and difference can also be performed on the set. The addAll() method, retainAll() method, and removeAll() method, in that order, carry out the operations.

The “JavaSetExample5” class is declared with the main() method where the mathematical operation is performed on the sets. First, we generate two integer arrays – “EvenNumber” and “OddNumber”. Then, we establish the HashSet class interfaces by declaring the “s1” and “s2” objects. Each element in the arrays “EvenNumber” and “OddNumber” in the sets is added using the addAll() method. After that, we carry out the union operation. We define the “union” object where the set is implemented and added all the elements of “s1” set.

Then, we have the addAll() method with the “union” object where the “s2” set elements are added. The addAll() method adds only those elements of set “s1” which are not found in the set “s1” and returns the union of the set “s1” and “s2”.

The intersection operation is then performed on both “s1” and “s2” sets. For this, we create the set interface again in the “intersection” object where the set “s1” elements are added. We use the retainAll() method which takes the set “s2” as an argument. This method gets all those elements from the set “s2”. The intersects values are obtained from the “s1” and “s2” sets.

Next, we have the difference operation on the “s1” and “s2” sets. We first create the set by defining the “difference” object and pass all the elements of the set “s1” to it. Then, we call the “difference” object but with the removeAll() method. The removeAll() method takes the set “s2” as input and removes all the elements from the set “s2”. Only the difference operation results are displayed.

On the following screen, the outcomes of the set are shown:

Conclusion

Java does not allow for the direct instantiation of “java.util.set” because it is merely an interface. Consequently, users must generate a Java class that implements the Set interface. We discussed the Java set along with its few methods. All the methods provided by Java sets play the very important functionalities. Moreover, the last example shows the mathematical operation on the set.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.