TreeMap 是否适用于具有数百万个不同记录的输入,其中定期按值排序

Is TreeMap suitable for a input with million distinct records where sorting by value occurs regularly

我打算在下面的场景中使用树图。

Given a list of users(around million distinct users) I need to keep track of the number of visits for each one of them.
The users can visit the same site multiple times. It is a single machine and in memory I need to maintain a ranking of each user based on no of visits in the map or any other data structure I will be using.
For the code written the method of getting the user ranking and altering the view count and sorting again would be called more frequently than the new users visiting.
The ranking of the user will be again based on view count which would be the value in my TreeMap and key would be the distinct user id.
Every time the view count changes for a user the value is changed for the respective id and I need to sort the huge TreeMap again.
Is there any other datastructure which would help me maintain a huge sorted list of users based on their visit count or is the TreeMap good enough.

简答:没有。 TreeMap 并不意味着按值排序。

您可以根据每个用户的观看次数使用优先级队列。它允许在 O(log(n)) 中高效地插入。另外,您可以将 O(log(n)) 中的视图计数更改为(decreaseKey/increaseKey 操作)

对于检索用户的观看次数,它比较复杂,因为此 DS 不允许高效查找。但是您可以维护一个用户的 HashMap,这样您就可以在 O(1) 时间内访问一个用户并获得查看次数。但是,由于观看次数是一个可变字段,因此不得使用它来计算用户的哈希值。