Al Davis wrote:I know that HashMaps can't be sorted using the Collections.sort methods, but the problem specified the map structure I've posted ( FilmComparator(Map<Film, List<Rating>> ratings) ) and I'm stuck with it.
Henry Wong wrote:The problem specified that you need to use a Map ... but where does it say that you have to use a HashMap? Why can't you use a Map type that can be sorted?
That is strange too. It is not usual for a Comparator to have any fields. It is usually implemented as a class with one method (compare(T, T)). I can see how you might want a class that will sort your Films, but that isn't the usual use of a Comparator<Film>. I think, maybe you should query the requirements with whoever gave you them.
In your first post, you wrote:The class FilmComparator has to implement the interface Comparator<Film>, and it has to have the constructor public FilmComparator(Map<Film, List<Rating>> ratings).
That is my idea of how you use a Comparator. So why pass a Map to its constructor?
Piet Souris wrote:. . . come up with a class that implements a Comparator<Film> . . .
Campbell Ritchie wrote:I was thinking the idea of the Map passed to the constructor was simply to provide access to the Rating lists.
Presumably it would be all right to take all the films out of a hash map and use those averages to sort it into a tree map (or similar)?
But what is going to happen if you take the Films out of the Map and concurrently scores are added to a film which later alter the sorting?
That is what I expected; it sounds a bad idea all round. Once that undefined behaviour, which I think I can construe as “incorrect ordering,” starts, the correct performance of such a program is doomed. whether single‑lthreaded or concurrent.
Stephan van Hulst wrote:. . . Once you alter the values of the map, the content of your TreeMap will be completely undefined if it uses a comparator based on values of another map.
Piet Souris wrote:Lastly but one: if the ratings map gets updated, it is then simply a matter of creating a new instance of the FilmComparator, or you could give that class a method like 'setNewMap'.
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Javahttps://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire