• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I efficiently print the elements of a Set in descending order?  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is an old assignment I have. I have not worked on it ever and now I have nobody for nitpicking. Since I'm currently trying to understand the Collection API, I thought I'd give it a try. Following is one of the three requirements I have.

Req 1 : You have to create a program that simulates the Score chart of a computer game. It should list top players of the game with their scores.

The score chart maintains scores of top 100 players only - the name of the player and his score.

A player can play as many games as he wants. There should be an entry in the score chart for each top score. A player can have two entries if he has two top scores. Two or more players can have multiple entries for the same score as long as the score is one of the top 100 scores. A player cannot have two entries for the same score. The score chart maintains its entries in the descending order of the scores. In the event the score chart already has 100 entries and a new player gets the same score as the minimum score on the chart, no new entry should be made.

Since the number of players that play the game far outnumber the number of entries on the score chart, use a data structure that can minimize the time taken to keep the score chart updated with the top scores.


I think I have solved most of the requirement 1 correctly. I have used a TreeSet for it. Do you think, the choice of the data structure is right? What should be the efficient way to print the scores in the descending order? Is it to do with converting the Set to a List/Array?

Here is my code.





Here is the output for Req 1-
true
true
true
true
true
true
true
true
true
true
[seven ; 5, ten ; 5, four ; 10, five ; 70, nine ; 100, two ; 100, six ; 800, one ; 1000, three ; 5000, eight ; 9000]
true
[ten ; 5, four ; 10, five ; 70, nine ; 100, two ; 100, six ; 800, eleven ; 1000, one ; 1000, three ; 5000, eight ; 9000]
false
[ten ; 5, four ; 10, five ; 70, nine ; 100, two ; 100, six ; 800, eleven ; 1000, one ; 1000, three ; 5000, eight ; 9000]
BUILD SUCCESSFUL (total time: 0 seconds)

I think the only thing I need to change in the above program is the printing of the elements, which should be in the descending order of the scores. Please help.

Thanks,
Chan.



 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

TreeSet has method descendingIterator() for iterating from the greatest element to the least.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Volodymyr,

I changed my Set declaration as follows.


and used the descendingIterator. Tried descendingSet too.

I was not able to see these methods earlier cause I had declared my set as Set type.

Resolved!

Chan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!