Results 1 to 6 of 6

Thread: Uniqueness of a TreeSet

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Uniqueness of a TreeSet

    Hello,
    I have a set of objects that are affecting the scores and I need a structure that allows me to ensure the uniqueness of objects and order them. For cons, the uniqueness is on the subject and covers score.
    So I created a class that contains the object and its score:
    Code:
    class element {
        private Object ob;
        private double sc;
    
        Public boolean equals(Object o) {
            return ob.equals(((element)o).getObjet());
        }
    }
    And i am getting an error for this, any one has any idea about this.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Uniqueness of a TreeSet

    Hello,
    There is no reason why this does not work. Regarding the javadoc, it does not say that the method compares (or compareTo if you implement Comparable) must be consistent with the equals method, she said:general it should be.
    It is generally the case, but not strictly required that (compare (x, y) == 0) == (x.equals (y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator taxed orderings that are inconsistent with equals."
    You're quite right to do what you do want.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Uniqueness of a TreeSet

    Hello,
    In your equals method, you should test first whether the class is an instance of our elements .. otherwise you may have a ClassCastException. In addition, you based on the equals method of the object class, which just COPARE references and not values, should enrich your equals method.
    the contract must respect it is:

    - X.equals (y) = true -> y.equals (x) = True
    - X.equals (y) x.equals and (z) = true -> x.equals (z) = true
    - X.equals (x) = true
    - X.equals (y) returns the same value regardless the context of the execution ..
    - X.equals (null) returns false

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Uniqueness of a TreeSet

    Hello,
    in your equals method, you should test first whether the class is an instance of your elemnts .. otherwise you may have a ClassCastException ...
    Maybe just a glimpse of equals he wrote.
    In addition, you based on the equals method of the object class, which just COPARE references and not values. should enrich your equals method. Ben if reference objects that override equals what will be the equals of those objects to be called. Put the string in the elements and it will be equal String will be taken.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Uniqueness of a TreeSet

    Hello,
    I read this from the suns official site. I have not understood it correctly.Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consist with equals if it is to correctly implement Implements the Set interface. (See Comparable or Comparator for a precise definition of consist with equals.) This is so Because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two Deemed keys that are equal by this method are, from the standpoint of the set, equal. And I need to respect the interface Set for uniqueness on objects is not on the scores. Any help on this will be highly appreciated.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Uniqueness of a TreeSet

    Hello,
    For what is your problem, if you want to fully meet the API set (note that the use of equals and equals compares different breeds also a collection type Set uniqueness within the meaning of objects) and then use your object instances through the interface and set its iterator method, I think the best would be to override an implementation of Set, HashSet instance and override iterator to return an iterator that has ordered the items on the index. If you find other solutions please feel free to post them this could be interesting.

Similar Threads

  1. TreeSet Comparator in java
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 05:27 AM
  2. List of TreeSet in JAVA
    By Hajra in forum Software Development
    Replies: 3
    Last Post: 27-10-2009, 07:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,938,000.34851 seconds with 16 queries