Best Data Structure for fast retrieval, update, and keeping ordering

Hong Wei Wang :

The problem is as follows

  • I need to keep track of url + click count.
  • I need to be able to update url quickly with click count when user click on that url.
  • I need to be able to retrieve the top 10 click count URL quickly.

NOTE: Assuming you cannot use the database.

What is the best data structure to achieve the result?

I have thought about using a map before, but map doesn't keep track of ordering of the top 10 clicks.

laune :

You need an additional List<Map.Entry<URL,Integer>> for holding the top ten, with T being the click count for the lowermost.

  • If you count another click and this count is still not greater than T: do nothing.
  • If the increased count is greater than T, check whether the URL is in the list or not. If it is, do nothing. If it is not, add this entry to the List, sort and delete the last entry if the list has more than 10 entries. Update T.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=468838&siteId=1
Recommended