How To Declare Set In Java
Fix in Java
The ready 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 equally a collection interface. The set interface is used to create the mathematical ready. 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 prepare implementation.
In the in a higher place diagram, the NavigableSet and SortedSet are both the interfaces. The NavigableSet extends the SortedSet, so information technology volition not retain the insertion order and shop the information in a sorted way.
SetExample1.coffee
Output:
Note: Throughout the section, we have compiled the programme with file proper name and run the program with grade name. Because the file name and the class name are different.
Operations on the Set Interface
On the Set up, we can perform all the basic mathematical operations like intersection, spousal relationship and difference.
Suppose, we take two sets, i.east., set1 = [22, 45, 33, 66, 55, 34, 77] and set2 = [33, 2, 83, 45, iii, 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 exist [33, 45, 55].
- Union: The union operation returns all the elements of set1 and set2 in a single set, and that set can either exist set1 or set2. The spousal relationship of set1 and set2 volition be [2, 3, 12, 22, 33, 34, 45, 55, 66, 77, 83].
- Divergence: The difference performance deletes the values from the set which are present in another set up. The deviation of the set1 and set2 will be [66, 34, 22, 77].
In set, addAll() method is used to perform the marriage, retainAll() method is used to perform the intersection and removeAll() method is used to perform departure. Let'south take an example to empathise how these methods are used to perform the intersection, spousal relationship, and deviation operations.
SetExample2.java
Output:
Description:
In the above code, first, we create two arrays, i.e., A and B of blazon integer. After that, we create two gear up, i.e., set1 and set2 of type integer. We catechumen both the array into a listing and add the elements of array A into set1 and elements of assortment B into set2.
For performing the union, we create a new ready union_data with the same element of the set1. We so phone call the addAll() method of fix and pass the set2 as an statement to it. This method volition 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 prepare intersection_data with the same element of the set1. Nosotros and then call the retainAll() method of set and pass the set2 every bit an argument to it. This method will go all those elements from the intersection_data which are present in set2 and shop it in the intersection_data. At present, the intersection_data contains the intersect value of both the sets.
For performing the divergence, we create a new set difference_data with the aforementioned element of the set1. We so call the removeAll() method of set and laissez passer the set2 equally an argument to information technology. 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
At that place are several methods available in the set interface which we tin can utilize to perform a certain functioning on our sets. These methods are as follows:
1) add()
The add() method insert a new value to the prepare. The method returns truthful and false depending on the presence of the insertion element. It returns false if the element is already present in the gear up and returns truthful if it is not present in the set.
Syntax:
SetExample3.java
Output:
2) addAll()
The addAll() method appends all the elements of the specified collection to the set.
Syntax:
SetExample4.java
Output:
3) clear()
The method removes all the elements from the fix. It doesn't delete the reference of the set. It simply deletes the elements of the prepare.
Syntax:
SetExample5.coffee
Output:
4) contains()
The contains() method is used to know the presence of an element in the set. Its render value is truthful or simulated depending on the presence of the chemical element.
Syntax:
SetExample6.java
Output:
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 fifty-fifty if 1 of the elements is missing in the existing set.
Syntax:
SetExample7.java
Output:
half dozen) hashCode()
The method is used to derive the hash code value for the current instance of the gear up. Information technology returns hash lawmaking value of integer type.
Syntax:
SetExample8.java
Output:
7) isEmpty()
The isEmpty() method is used to identify the emptiness of the set up . It returns true if the fix is empty and returns imitation if the set is not empty.
Syntax:
SetExample9.coffee
Output:
eight) iterator()
The iterator() method is used to find the iterator of the prepare. The iterator is used to get the element one by i.
Syntax:
SetExample10.java
Output:
9) remove()
The method is used to remove a specified element from the Ready. Its render value depends on the availability of the element. Information technology returns truthful if the element is bachelor in the set and returns imitation if it is unavailable in the set.
Syntax:
SetExample11.java
Output:
xi) removeAll()
The method removes all the elements of the existing set from the specified collection.
Syntax:
SetExample12.java
Output:
xi) retainAll()
The method retains all the elements from the set specified in the given collection.
Syntax:
SetExample13.java
Output:
12) size()
The method returns the size of the set.
Syntax:
SetExample14.java
Output:
13) removeAll()
The method is used to create an assortment with the same elements of the set.
Syntax:
SetExample15.coffee
Output:
How To Declare Set In Java,
Source: https://www.javatpoint.com/set-in-java
Posted by: pottsgoves1986.blogspot.com

0 Response to "How To Declare Set In Java"
Post a Comment