Showing posts with label Collections - Hashset. Show all posts
Showing posts with label Collections - Hashset. Show all posts

Thursday, October 20, 2011

Check if a particular element exists in Java HashSet

   package org.best.example;
    /*
      Check if a particular element exists in Java HashSet Example
      This Java Example shows how to check whether an elements is contained in Java
      HashSet using contains method.
    */
    
    import java.util.HashSet;
    
    public class CheckElementHashSetExample {
    
      public static void main(String[] args) {
    
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
    
        /*
          To check whether a particular value exists in HashSet use
          boolean contains(Object value) method of HashSet class.
          It returns true if the HashSet contains the value, otherwise false.
        */
      
        boolean blnExists = hSet.contains(new Integer("3"));
        System.out.println("3 exists in HashSet ? : " + blnExists);
    
      }
    }
    
    /*
    Output would be
    3 exists in HashSet ? : true
    */

Wednesday, October 19, 2011

Copy all elements of Java HashSet to an Object Array

   package org.best.example;
    /*
      Copy all elements of Java HashSet to an Object Array Example
      This Java Example shows how to copy all elements of Java HashSet object to an
      array of Objects using toArray method.
    */
    
    import java.util.HashSet;
    
    public class CopyElementsOfHashSetToArrayExample {
    
      public static void main(String[] args) {
    
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
      
        /*
          To copy all elements of java HashSet object into array use
          Object[] toArray() method.
        */
      
        Object[] objArray = hSet.toArray();
      
        //display contents of Object array
        System.out.println("HashSet elements are copied into an Array.
                                                       Now Array Contains..");
        for(int index=0; index < objArray.length ; index++)
          System.out.println(objArray[index]);
      }
    }
    
    /*
    Output would be
    HashSet elements are copied into an Array. Now Array Contains..
    3
    2
    1
    */

Tuesday, October 18, 2011

Get Size of Java HashSet

   package org.best.example;
    /*
      Get Size of Java HashSet Example
      This Java Example shows how to get the size or nubmer of elements stored in
      Java HashSet object using size method.
    */
    
    import java.util.HashSet;
    
    public class GetSizeOfJavaHashSetExample{
    
      public static void main(String[] args) {
      
        //create HashSet object
        HashSet hSet = new HashSet();
      
        /*
          To get the size of HashSet use
          int size() method of HashSet class. It returns the number of elements
          stored in HashSet object.   
        */
        System.out.println("Size of HashSet : " + hSet.size());
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
    
        System.out.println("Size of HashSet after addition : " + hSet.size());
      
        //remove one element from HashSet using remove method
        hSet.remove(new Integer("1"));
        System.out.println("Size of HashSet after removal : " + hSet.size());
      }
    }
    
    /*
    Output would be
    Size of HashSet : 0
    Size of HashSet after addition : 3
    Size of HashSet after removal : 2
    */

Monday, October 17, 2011

Iterate through elements of Java HashSet

   package org.best.example;
    /*
      Iterate through elements of Java HashSet example
      This Java Example shows how to iterate through elements Java HashSet object.
    */
    
    import java.util.HashSet;
    import java.util.Iterator;
    
    public class IterateThroughElementsOfHashSetExample {
    
      public static void main(String[] args) {
    
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
      
        //get the Iterator
        Iterator itr = hSet.iterator();
      
        System.out.println("HashSet contains : ");
        while(itr.hasNext())
          System.out.println(itr.next());
      }
    }
    
    /*
    Output would be
    HashSet contains :
    3
    2
    1
    */

Sunday, October 16, 2011

Remove specified element from Java HashSet

   package org.best.example;
    /*
      Remove specified element from Java HashSet example
      This Java Example shows how to remove a specified object or element
      contained in Java HashSet object using remove method.
    */
    
    import java.util.HashSet;
    
    public class RemoveSpecifiedElementFromHashSetExample {
    
      public static void main(String[] args) {
    
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
      
        System.out.println("HashSet before removal : " + hSet);
      
        /*
          To remove an element from Java HashSet object use,
          boolean remove(Object o) method.
          This method removes an element from HashSet if it is present and returns
          true. Otherwise remove method returns false.
        */
      
        boolean blnRemoved = hSet.remove(new Integer("2"));
        System.out.println("Was 2 removed from HashSet ? " + blnRemoved);
      
        System.out.println("HashSet after removal : " + hSet);
      }
    }
    
    /*
    Output would be
    HashSet before removal : [3, 2, 1]
    Was 2 removed from HashSet ? true
    HashSet after removal : [3, 1]
    */

Saturday, October 15, 2011

Remove all elements from Java HashSet

   package org.best.example;
   /*
      Remove all elements from Java HashSet example
      This Java Example shows how to remove all elements contained in Java HashSet
      or clear HashSet object using clear or removeAll methods. It also shows how
      to check whether HashSet object is empty or not using isEmpty method.
    */
    
    import java.util.HashSet;
    
    public class RemoveAllElementsFromHashSetExample {
    
      public static void main(String[] args) {
    
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        //add elements to HashSet object
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
      
        System.out.println("HashSet before removal : " + hSet);
      
        /*
          To remove all elements from Java HashSet or to clear HashSet object use,
          void clear() method.
          This method removes all elements from HashSet.
        */
      
        hSet.clear();
        System.out.println("HashSet after removal : " + hSet);
      
        /*
          To check whether HashSet contains any elements or not use
          boolean isEmpty() method.
          This method returns true if the HashSet does not contains any elements
          otherwise false.
        */
      
        System.out.println("Is HashSet empty ? " + hSet.isEmpty());   
      
        /*
          Please note that removeAll method of Java HashSet class can also be
          used to remove all elements from HashSet object.
        */
      }
    }
    
    /*
    Output would be
    HashSet before removal : [3, 2, 1]
    HashSet after removal : []
    Is HashSet empty ? true
    */

Friday, October 14, 2011

Simple HashSet Example

   package org.best.example;
   /*
      Simple Java HashSet example
      This simple Java Example shows how to use Java HashSet. It also describes how to
      add something to HashSet object using add method.
    */
    
    import java.util.HashSet;
    
    public class SimpleHashSetExample {
    
      public static void main(String[] args) {
        //create object of HashSet
        HashSet hSet = new HashSet();
      
        /*
          Add an Object to HashSet using
          boolean add(Object obj) method of Java HashSet class.
          This method adds an element to HashSet if it is not already present in HashSet.
          It returns true if the element was added to HashSet, false otherwise.
        */
      
        hSet.add(new Integer("1"));
        hSet.add(new Integer("2"));
        hSet.add(new Integer("3"));
      
        /*
          Please note that add method accepts Objects. Java Primitive values CAN NOT
          be added directly to HashSet. It must be converted to corrosponding
          wrapper class first.
        */
      
        System.out.println("HashSet contains.." + hSet); 
      }
    }
    
    /*
    Output of the program would be
    HashSet contains..[3, 2, 1]
    */

Thursday, September 22, 2011

Check duplicated value in array

package org.best.example;
 
import java.util.Set;
import java.util.HashSet;
 
public class CheckDuplicate
{
 public static void main(String args[])
 {
  String [] sValue = new String[]{"a","b","c","d","","","e","a"};
 
  if(checkDuplicated_withNormal(sValue))
   System.out.println("Check Normal : Value duplicated! \n");
  if(checkDuplicated_withSet(sValue))
   System.out.println("Check Set : Value duplicated! \n");
 
 }
 
 //check duplicated value
 private static boolean checkDuplicated_withNormal(String[] sValueTemp)
 {
  for (int i = 0; i < sValueTemp.length; i++) {
   String sValueToCheck = sValueTemp[i];
   if(sValueToCheck==null || sValueToCheck.equals("")) 
             continue; //empty ignore
   for (int j = 0; j < sValueTemp.length; j++) {
     if(i==j)continue; //same line ignore
     String sValueToCompare = sValueTemp[j];
     if (sValueToCheck.equals(sValueToCompare)){
       return true;
     }
   }
 
  }
  return false;
 
 }
 
 //check duplicated value
 private static boolean checkDuplicated_withSet(String[] sValueTemp)
 {
  Set<String> sValueSet = new HashSet<String>();
  for(String tempValueSet : sValueTemp)
  {
   if (sValueSet.contains(tempValueSet))
    return true;
   else
    if(!tempValueSet.equals(""))
     sValueSet.add(tempValueSet);
  }
  return false;
 }
  }