• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

sortedhashset Error

I guess some of you may have seen this before.
I have a class, SortedHashSet.
It is supposed to incorporate both TreeSet and HashSet.
I have to override the add function such that it will maintain a TreeSet and a HashSet.

ok there's my code

class SortedHashSet<T> extends TreeSet<T> {
      private HashSet<T> hashobject;
      public SortedHashSet (){
            hashobject = new HashSet<T>();
      }
      
      public boolean add(T item){
            super.add(item);
            hashobject.add(item);
            return true;
      }
      
      public void clear(){
            super.clear();
            hashobject.clear();
      }
      
      public boolean contains(Object item){
            super.contains(item);
            hashobject.contains(item);
            return true;
      }
      
      public boolean isEmpty(){
            super.isEmpty();
            hashobject.isEmpty();
            return true;
      }
      
      public boolean remove(Object item){
            super.remove(item);
            hashobject.remove(item);
            return true;
      }
}

and i got these errors which are not compile errors

isEmpty returned true on set with one item!
isEmpty returned true on set with two items!
Contains item that has not been added.
isEmpty returned true after adding duplicate!
Contains item that has not been added after adding duplicate.
Contains item that has not been added after adding many items.
Contains item that has not been added after addAll.
Problem with containsAll method when false.
isEmpty returned true on non-empty set.
Problem removing from set (per contains).
Wrong return value from remove after failure.
Problem with clone() after remove (per contains).
Problem with Iterator.remove() (per contains).
Wrong return value from removeAll after failure.
Problem with retainAll (per toString).
Wrong return value from retainAll after success.
1 Problem finding item.
contains() failed to use hashing in headSet.

Can anyone help me with this?
0
solance
Asked:
solance
  • 4
3 Solutions
 
CEHJCommented:
>>isEmpty returned true on set with one item!

Because you return a hard-coded true
0
 
CEHJCommented:
(... which you do throughout )
0
 
hoomanvCommented:
change all to somthing like this
     public boolean isEmpty(){
          return super.isEmpty() && hashobject.isEmpty();
     }
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CEHJCommented:
You might like to do something like this until you're sure of your code:

public boolean isEmpty() {
      int hashEmpty = hashobject.isEmpty()? 1 : 0;
      int setEmpty = super.isEmpty()? 1 : 0;
      // Check in same state
      assert (hashEmpty ^ setEmpty) == 0;
      return  (hashEmpty & setEmpty) > 0? true : false;
}
0
 
objectsCommented:
you don't need to override isEmpty() as both set contain the same set of elements
i.e you can delete isEmpty() method from your class.
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now