sortedhashset Error

Posted on 2006-05-11
Last Modified: 2010-03-31
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?
Question by:solance
    LVL 86

    Expert Comment

    >>isEmpty returned true on set with one item!

    Because you return a hard-coded true
    LVL 86

    Expert Comment

    (... which you do throughout )
    LVL 14

    Accepted Solution

    change all to somthing like this
         public boolean isEmpty(){
              return super.isEmpty() && hashobject.isEmpty();
    LVL 86

    Assisted Solution

    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;
    LVL 92

    Assisted Solution

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

    Expert Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This video teaches viewers about errors in exception handling.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now