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 7906541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:01:25+00:00 2026-06-03T11:01:25+00:00

I need to sort a java collection of objects by an Integer value level.

  • 0

I need to sort a java collection of objects by an Integer value “level”. I also need to determine if this collection already contains an object by “title”.

I believe the best choice of a collection is a TreeSet to have an ordered set of unique values.

I have an object with the “level” and “title attributes. It implements comparable like so:

It overrides the Equals method (for checking if the object is already contained in the TreeSet by “title”.

The code looks like:

@Override
public boolean equals(Object arg0) {

    Artifact obj = (Artifact) arg0;

    if (this.getTitle().equals(obj.getTitle())) { 
        return true;
    }

    return false;
}

@Override
public int compareTo(Artifact aThat) {  

    final int BEFORE = -1;
    final int EQUAL = 0;
    final int AFTER = 1;

    if (this == aThat) return EQUAL;

    if (this.level < aThat.level) return BEFORE;
    if (this.level > aThat.level) return AFTER;

    assert this.equals(aThat) : "compareTo inconsistent with equals.";
    return EQUAL;
}

When I attempt to add values to the list from an arraylist with possibly duplicate values. The contains seems to not work and objects are added to the TreeSet regardless. Here is the code:

TreeSet<Artifact> subsetOfArtifacts = new TreeSet<Artifact>();

ArrayList<Artifact> allArtifacts = getArtifacts(); 
Iterator<Artifact> allArtifactsIter = allArtifacts.iterator();

while (allArtifactsIter.hasNext()) {
    Artifact artifact = (Artifact) allArtifactsIter.next();
    if (!subsetOfArtifacts.contains(artifact)) {
        subsetOfArtifacts.add(artifact);
    }
 }

I want to ideally have a list of all unique artifacts sorted by level. How do I accomplish this?

  • 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-06-03T11:01:26+00:00Added an answer on June 3, 2026 at 11:01 am

    If you override equals() you should override hashCode() too! Otherwise, behaviour with collections, especially Sets, is undefined. You should add this method to your class:

    @Override
    public int hashCode() {
        return title.hashCode();
    }
    

    Next, you should use a HashSet instead and use Set sortedSet = new TreeSet(set); for the sorting. Once you do that, it should all work OK.

    The reason is that HashTables rely on the fact that if two objects are equal() then their hashCode() is also equal. Here’s an excerpt from the javadoc for hashCode()

    The general contract of hashCode is:

    • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently
      return the same integer, provided no information used in equals
      comparisons on the object is modified. This integer need not remain
      consistent from one execution of an application to another execution
      of the same application.
    • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must
      produce the same integer result.
    • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on
      each of the two objects must produce distinct integer results.
      However, the programmer should be aware that producing distinct
      integer results for unequal objects may improve the performance of
      hashtables.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort the list in java as below: List contains collection of
I need to sort a collection of objects by a utf-8 string property (built
I have a collection of objects in JavaScript like this: Object collection = new
I need to sort an array that can look like this: $array[4][0] = array('id'
hello guys i need to sort some elements of integer in an integer array
I have a large text file I need to sort in Java. The format
This is homework, but I need a nudge. I can't find how to sort
I need some sort of solution in Java for the following requirements: Search in
I need to sort a list who's items are objects that describe how the
I need to sort an array of ints using a custom comparator, but Java's

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.