1

I have the following simple class, containing a String and an Int.

private static class SuggestionAndScore
{
    private String suggestion; 
    private int score;  
}

I will have a List of these objects which may contain up on 500,000 items. What is the best way to sort this based on the value of score? Should I have the class implement Comparator and use Collections.sort on it or it there a better way? Performance is critical so hence my question as I want to make sure I get the best solution.

5
  • 9
    1. Make it work (with your suggestion to use a Comparator with Collections.sort) 2. Profile 3. If performance is not good enough, optimise. Commented Nov 22, 2012 at 10:46
  • 3
    How do you know performance is critical without implementing a solution first? Start with Collections.sort() and if it isn't good enough, then you can start think about better alternatives. Commented Nov 22, 2012 at 10:47
  • If sorting is not fast enough, wonder if you really need everything to be sorted. It is possible to get the n-th highest scores without sorting everything. Commented Nov 22, 2012 at 10:55
  • The list will be immutable once it is created. Thing is most of the time it will have < 1000 items but it MAY have up to 500,000. As I don't want any memory issues or unacceptable performance issues with large numbers I guess it's a compromise I need. Comparator works fine with 500,000. Would using a Tree Set be easier on memory resources? Commented Nov 22, 2012 at 11:24
  • 1
    You might be surprised about how fast an in memory sort of 500,000 items is, especially if the object comparison is simply on an int. Commented Nov 22, 2012 at 11:25

2 Answers 2

1

One needs to understand that the sorting methods loads the entire collection into memory, so in case your SuggestionAndScore object is big (probably because of the big suggestion string), you will consume huge amount of memory and could potentially even crash the system. If you believe memory could be a concern then use in-place sorting. If memory isnt a problem then use TreeSet.

Sign up to request clarification or add additional context in comments.

Comments

0

If all your objects are unique then use TreeSet. Gives the best performance since the objects are already stored in sorted order. (You need not even call Collections.sort())

3 Comments

Depending on the requirements, using a TreeSet could easily be an overkill here.
@Jan your statement is highly debatable. Depending on the requirements, using a TreeSet could give great performance.
@PC It isn't debatable at all. It all depends on how many times you're going to modify the list. If the answer is never, TreeSet is an overkill. If you build the list dynamically, TreeSet is the better choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.