• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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