creating your own HashSet class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: creating your own HashSet class

  1. #1
    Join Date
    Jan 2006
    Location
    Lourdes, Lubao, Pampanga, Philippines
    Posts
    36

    Unhappy creating your own HashSet class

    hello everyone...

    I'm new in java and is currently studying about collections...

    I'm trying to make my own collection package so that I would understand more about the functions of the collection classes and interfaces....

    I'm currently making my own HashSet class and I have a problem about preventing duplicates...here the code...


    Code:
    // The IMySet interface has the following abstract method(s)
    // public void clear();
    // public void remove(Object obj);
    // and implements the IMyCollection interface and here are its abstract method(s)
    // public void add(Object obj)
    // public void add(IMyCollection collection)
    
    // here's my code for my custom HashSet class[/B]
    
    public class CMyHashSet implements IMySet {
    
      // attribute(s)
      private Object[] object;
      private boolean[] isShown;
      private int counter;
    
      // constructor(s)
      public CMyHashSet() {
    	  object = new Object[16];
        isShown = new boolean[16];
    	} // close CMyHashSet()
    
      // implemented method(s)
      public void add(Object obj) {
    
    	  if ( addObject(obj) ) {
          object[counter] = obj;
          isShown[counter] = true;
          counter++;
    	  } // close if (...)
    
    	} // close add(Object)
    
      public void add(IMyCollection collection) {
    
    	  if ( addObject(collection) ) {
    	  	object[counter] = collection;
    	  	isShown[counter] = true;
    	  	counter++;
    	  } // close if (...)
    
    	} // close add(Collection)
    
      public void remove(Object obj) {
        // haven't done yet
    	} // close remove(Object)
    
      public void clear() {
    
        object = new Object[16];
        isShown = new boolean[16];
        counter = 0;
    
    	} // close clear()
    
    	public String toString() {
    
        StringBuffer my_string = new StringBuffer();
    
        my_string.append( '<' );
        for (int i = 0; i < object.length; i++) {
    
          if ( isShown[i] ) {
    		    my_string.append( object[i] );
    
    		    if ( i < countIsShown() - 1 ) {
    	        my_string.append( ", " );
    			  } // close if (...)
    
    			} // close if (...)
    
    		} // close for (...)
        my_string.append( '>' );
    
        return ( my_string.toString() );
    
    	} // close toString()
    
      // other method(s)
    
      // count how many true values isShown[] has
      private int countIsShown() {
    
    	  int numOfTrueValue = 0;
    
        for (int i = 0; i < isShown.length; i++) {
    
    		  if ( isShown[i] ) {
            numOfTrueValue++;
    			} // close if (...)
    
    		} // close for (...)
    
    	  return ( numOfTrueValue );
    
    	} // close countIsShown()
    
      // checks if an Object has duplicates with
      // one of the Objects of the object[]
      private boolean addObject(Object obj) {
    
    	  boolean isAdded = true;
    
    	  for (int i = 0; i < object.length; i++) {
    
    		  if ( object[i] != null &&
    		    object[i].hashCode() == obj.hashCode() ) {
    			  isAdded = false;
     				break;
    			} // close if (...)
    
    		} // close for (...)
    
        return ( isAdded );
    
    	} // close addObject(Object)
    
    } // close CMyHashSet class
    my problem is that my custom HashSet class doesn't let null values in the object[]...does anyone has a suggestion on what I should do?...tnx...[/B]
    Last edited by Hack; 08-10-2007 at 12:42 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    What does null values have to do with preventing duplicate values? In your add method, check to see if your current set already includes the object to be added.

Similar Threads

  1. Creating Base Logging Class
    By sandy_man_tx in forum Java
    Replies: 3
    Last Post: 02-27-2006, 06:02 PM
  2. Replies: 2
    Last Post: 08-23-2005, 02:19 PM
  3. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  4. Error creating Class
    By Tony Ogilvie in forum Java
    Replies: 1
    Last Post: 08-29-2002, 01:59 PM
  5. Replies: 205
    Last Post: 09-26-2001, 01:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center