The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.
The methods declared by Set are summarized in the following table:
SN | Methods with Description |
---|---|
1 | add( ) Adds an object to the collection |
2 | clear( ) Removes all objects from the collection |
3 | contains( ) Returns true if a specified object is an element within the collection |
4 | isEmpty( ) Returns true if the collection has no elements |
5 | iterator( ) Returns an Iterator object for the collection which may be used to retrieve an object |
6 | remove( ) Removes a specified object from the collection |
7 | size( ) Returns the number of elements in the collection |
Example:
Set have its implementation in various classes like HashSet, TreeSet, HashSet, LinkedHashSet, Following is the example to explain Set functionlaity:import java.util.*; public class SetDemo { public static void main(String args[]) { int count[]={34, 22,10,60,30,22}; Set |
[amrood]$ java SetDemo [34, 30, 60, 10, 22] The sorted list is: [10, 22, 30, 34, 60] The First element of the set is: 10 The last element of the set is: 60 |
No comments:
Post a Comment