• 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){
            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();
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.

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