Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:08:26+00:00 2026-05-26T20:08:26+00:00

Please review code: /* Run1.java */ package test; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;

  • 0

Please review code:

/* Run1.java */
package test;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class Run1 
{
    static public void main(String[] args)
    {
        SortedSet<TestClass> s = new TreeSet<TestClass>(); 

        s.add( new TestClass("name1", 100) );
        s.add( new TestClass("name2", 10) );
        s.add( new TestClass("name3", 1) );
        s.add( new TestClass("name4", 10) );
        s.add( new TestClass("name5", 100) );

        Iterator<TestClass> it = s.iterator();

        while(it.hasNext())
        {
            TestClass t = it.next();
            System.out.println( t.name+' '+t.value );
        }
    }
}

/* TestClass.java */
package test;

public class TestClass implements Comparable<TestClass> 
{
    public String name;
    public int value;

    public TestClass(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public int compareTo(TestClass o) 
    {
        return this.value - o.value;
    }

    public boolean equals(Object o) 
    {
        if (!(o instanceof TestClass))
            return false;
        TestClass n = (TestClass)o;

        return this.name.equals(n.name);
    }

    public int hashCode() 
    {
        return 31*name.hashCode();
    }

    public String toString() 
    {
        return name;
    }
}

Print out

name3 1
name2 10
name1 100

as i see because compareTo used for checking to equality (when returned 0). But i need check for unique by field TestClass.name and only sort by TestClass.value

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:08:26+00:00Added an answer on May 26, 2026 at 8:08 pm

    The result of compareTo() and equals() need to be compatible in this case, which means that you need to take into account equality in the comparison. For example:

    public int compareTo(TestClass o) 
    {
        return (this.value == o.value) ? this.name.compareTo(o.name) : this.value - o.value;
    }
    

    which introduces a sub-order on name for objects with the same value, making the result compatible with your equals() implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please review my code. For every my Entity i've created a service class, where
Please review the example code below, I have a class file that is loaded
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I got another problem with the FCKEditor within ASP.NET MVC. Please review the code
Please review my code and give me your advise on this: XML file: content.xml
I have a memory leak at NSDate . Please review the code below. -(void)myMethods:(NSDate
Please review my brief example code below and tell me if there is better,
Would someone please review this code and tell me why the for loops are
Please review my .htaccess code below. The code below was suggested by I User
Please review the following GUI code. <?xml version=1.0 encoding=utf-8?> <RelativeLayout android:id=@+id/frameLayout1 android:layout_width=fill_parent android:layout_height=wrap_content xmlns:android=http://schemas.android.com/apk/res/android>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.