Results 1 to 6 of 6

Thread: TreeSet Comparator in java

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    TreeSet Comparator in java

    Hello,
    I have a problem in using the TreeSet in my program, when I try to compile it, I get an error.
    Here is my code:
    Code:
    Public class test implements Comparator
    {
     
        /**
         *
         */
     
        Public int compares(Object arg0, Object arg1)
        {
            can c0 = (can) arg0;
            can c1 = (can) arg1;
            
          double d = c1.getScoreApresEtapeHarmonisation() - C2.getScoreApresEtapeHarmonisation();
            
            if (d < 0) return +1;
            if (d> 0) return -1;
            if (== d 0)
            {
                double[] scr1 = c1.cal(); 
                double[] scr2 = c2.cal();
                
                / / Comparing scores weighted by dominance. 
                for ( int i = 1; I < 5; I + + ) 
                {
                    if (scr1[i] - ScorePondere2[2] < 0) return +1;
                    if (scr1[i] - ScorePondere2[2] > 0) return -1;
                }
            }
            return 0;
        }     
            
            }
      }

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: TreeSet Comparator in java

    Hello,
    Have you redefine the equals method, because if the ex-breaking are equals, there are chances that the TreeSet regarded as the objects are identical and it does not add. If it's not that you can always try using a different parameter in tone compared to differentiate the ex-breaking (the name of such candidate) If it does not always show us the class "can", it could perhaps enlighten us.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: TreeSet Comparator in java

    Hey,
    I am new to this concept , even i need a help in this , but I have a code with me , check it out.
    Code:
      Public boolean equals(Other object)
        {
            if (!(other instanceof can)) return false;
            can castOther = (can) other;
            return new EqualsBuilder().append(this.getID(), CastOther.getID()).isEqual();
        }

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: TreeSet Comparator in java

    Hello,
    In the javadoc says that the equals method must be consistent with compareTo. In fact the TreeSet uses the method of the comparator to sort, if the comparator returns 0 for its objects are identical and it does not add the new object. If you have unique IDs you could use them in your comparator to return a nonzero value in case of a tie. You classify them by ID in case of increasing equality. It is a good solution that respects the logic of the method of the comparator.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: TreeSet Comparator in java

    Hello,
    You can shorten your code something like this
    Code:
    return c1.getId() - C2-getId();
    Instead of using this
    Code:
    return 0;
    Hope this will help you, just have a try at it and see if it works for you, if not then post back your code and the error you get while compiling the code.

  6. #6
    Join Date
    Dec 2009
    Posts
    213

    Re: TreeSet Comparator in java

    Hello,
    Can I use in the code, something like this.
    Code:
    ArrayList arr = new ArrayList(dEtape.getCandidatsInscritEtape());
    Collections.fate(ArrayList new con());	
    		
    	/ / Get the iterator
    		iter = ArrayList.iterator();
    Is this going to help me, please advice. I am in need of this code and you will have to help me out. Thanks in advance.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Uniqueness of a TreeSet
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 03:55 AM
  3. 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,714,035,615.23633 seconds with 16 queries