Features of Set set:
The same element cannot be stored.
Also because it is an abstract interface: You cannot instantiate a set object directly. (Set S = New set ()) Error
This interface is mainly inherited from the collections interface, so there are some common methods of collection.
A common approach:
| sr.no. |
Method & Description |
| 1 |
Add () adding elements to the collection |
| 2 |
Clear () Remove all elements from the collection |
| 3 |
Contains () determines whether an element is contained in a collection |
| 4 |
IsEmpty () Determines whether the collection is empty |
| 5 |
Iterator () is used primarily for recursive collections and returns a iterator () object |
| 6 |
Remove () Removes a specific object from the collection |
| 7 |
Size () returns a collection of sizes |
The two most significant implementations of the set interface are: HashSet TreeSet
TreeSet: The elements inside will be sorted by default.
set<integer> test =NewTreeset<>();intA = 1;intb = 8;intc = 3; Test.add (a); Test.add (b); Test.add (c); //iterate through the collection test with the foreach traversal//Output Results: 1 3 8 for(Integer value:test) {System.out.print (value+" "); } //using iterator to implement traversalIterator<integer> value =test.iterator (); while(Value.hasnext ()) {ints =Value.next (); System.out.print (S+" ");} //Output Results: 1 3 8
Usage of set in Java