Search code examples
javasortingdata-structurestreemap

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


I am planning to use a tree map in the below scenario.

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.


Solution

  • Short answer: no. TreeMap is not meant to be sorted by value.

    What you can do is using a priority queue, based the view count of each user. It allows efficient insertion in O(log(n)). Plus, you can alter the view count in O(log(n)) to (decreaseKey/increaseKey operations)

    For retrieval of the view count of a user, it's more complicated because this DS doesn't allow efficient lookup. But you can maintain a HashMap of Users, so you have access to a User in O(1) time and get the view count. However, as the view count is a mutable field, it must not be used to compute the hash of a user.