A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
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:
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
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 SetEx {
public static void main(String[] args) {
AddSet as = new AddSet();
as.addset();
}
}
class AddSet{
public void addset(){
int data [] = {80,5000,50,20,90,100,102,100,1000,2000,1000,50};
Set<Integer> set = new HashSet<Integer>();
Set<Integer> sortedset = new TreeSet <Integer>();
Set<Integer> linkedhashset = new LinkedHashSet <Integer>();
for(int i=0;i<data.length;i++){
set.add(data[i]);
sortedset.add(data[i]);
linkedhashset.add(data[i]);
}
System.out.print("Set Example"+set);
System.out.print("\nSorted Set Example"+sortedset);
System.out.print("\nLinked Hash Set Example"+linkedhashset);
}
}
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:
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
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 SetEx {
public static void main(String[] args) {
AddSet as = new AddSet();
as.addset();
}
}
class AddSet{
public void addset(){
int data [] = {80,5000,50,20,90,100,102,100,1000,2000,1000,50};
Set<Integer> set = new HashSet<Integer>();
Set<Integer> sortedset = new TreeSet <Integer>();
Set<Integer> linkedhashset = new LinkedHashSet <Integer>();
for(int i=0;i<data.length;i++){
set.add(data[i]);
sortedset.add(data[i]);
linkedhashset.add(data[i]);
}
System.out.print("Set Example"+set);
System.out.print("\nSorted Set Example"+sortedset);
System.out.print("\nLinked Hash Set Example"+linkedhashset);
}
}
No comments:
Post a Comment