• 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){
            return true;
      public void clear(){
      public boolean contains(Object item){
            return true;
      public boolean isEmpty(){
            return true;
      public boolean remove(Object 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?
  • 4
3 Solutions
>>isEmpty returned true on set with one item!

Because you return a hard-coded true
(... which you do throughout )
change all to somthing like this
     public boolean isEmpty(){
          return super.isEmpty() && hashobject.isEmpty();
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!

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;
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.

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