Set

I -Interface c--class

  • Insertion order is not served. Duplicate objects are not allowed.
  • Set interface doesn’t have any new method. As per need, we can use collection interface methods.
  • Set object always displays the data in random order.
  • We can add the elements only at ending position.
  • We can fetch the data only in forward direction.
  • We can insert null values except TreeSet.
  • We can insert heterogeneous elements in all the collection implementation classes except TreeSet and TreeMap class.
  • hashCode() and equals() method will be used internally to identify weather object is duplicate or not.
  • If two object have same hash code value and equals() method is also returning true means that object is duplicate.
  • Set interface doesn’t provide get()method to retrieve the object. We can traverse only by iterator() method of Set interface or by for loop.

HashSet

  • HashSet class is implemented based on hash table. All elements will be inserted based on hash code value.
  • HashSet implements Clonnable and Serilizable interface but it does not implement RandomAccess interface. That is why, accessing the element randomly is not possible.
  • We will not get any error while adding duplicate elements. The add() method returns false if we try to insert duplicate elements.
  • The actions like insert, update and delete takes more time.
  • Duplicate objects are not allowed.
  • It is fast for searching and fetching objects.
public HashSet() 
{
 map = new HashMap<>(); 
}

Constructors of HashSet class

HashSet h=new HashSet():

it will create HashSet object with default capacity 16 and default filling ratio 0.75.

HashSet h=new HashSet(int initialcapacity):

it will create HashSet object with specified initial capacity and default filling ratio 0.75.

HashSet h=new HashSet(int initialcapacity, float filling Ratio):

it will create HashSet object with specified initial capacity and specified filling ratio.

HashSet h=new HashSet(Collection c):

it will create an equivalent HashSet object for the given collection. This constructor is used for interconversion of collection objects.

Filling Ratio:

After filling the specified ratio, a new hashSet object will be created so that performance is improved. Ex-filling ratio 0.75 means when the HashSet object will be filled by 75%, a new HashSet object will be created.

import java.util.*;
class Test246
{
public static void main(String[] args)
{
HashSet h=new HashSet();
h.add("John");
h.add("Z");
System.out.println(h.add("Z"));//false
h.add(100);
h.add(200);
h.add(null);
h.add(null);
h.add(null);
System.out.println(h);
}
}
//O/P-false,[null,100,John,200,Z] 
//Note: insertion order is not preserved.


import java.util.*;  
class Test247
{  
 public static void main(String args[])
 {  
  HashSet h1=new HashSet();  
  h1.add("GHI"); 
  h1.add("ABC");  
  h1.add("DEF");  
  System.out.println(h1.add("GHI"));// false  
  
  Iterator itr=h1.iterator();  
  while(itr.hasNext()){
   String s=(String)itr.next();	  
   System.out.println(s);  
  }  
 }  
}
//O/P-false,[ABC,DEF,GHI]

 
import java.util.*;  
class Test247
{  
 public static void main(String args[]){  
  HashSet h1=new HashSet();  
  h1.add("A"); 
  h1.add("B");
  h1.add("F");  
  h1.add("D"); 
  h1.add("E");  
  h1.add("C");    
  Iterator itr=h1.iterator();  
  while(itr.hasNext()){
   String s=(String)itr.next();	  
   System.out.println(s);  
  }  
 } 
}
//O/P-A,B,C,D,E,F
 



import java.util.*;  
class Test250
{  
 public static void main(String args[])
 {  
 HashSet h1=new HashSet();  
  h1.add(new Integer(2)); 
  h1.add(new Integer(4));
  h1.add(new Integer(1));  
  h1.add(new Integer(5)); 
  h1.add(new Integer(3));  
  h1.add(new Integer(6));   
 System.out.println(h1);  
 Iterator itr=h1.iterator();  
  while(itr.hasNext()){
   Number n=(Number)itr.next();	  
   System.out.println(n);  
}
}   
//O/P-[1,2,3,4,5,6]  1,2,3,4,5,6


 
Footer with Map

Top