Do not use map or list, whether it is ArrayList or LinkedList, because ArrayList is equivalent to an array, and ranking changes require updating a large number of elements. Although LinkedList is more convenient for insertion and deletion, binary search cannot be used and it is necessary to find the changed elements. The location needs to be traversed. A better implementation is to use a balanced binary tree, or directly use sortset
Why use map? In this way, all the data after the inserted ranking must be adjusted every time. Isn’t it possible to use a list? Use list.add(int, object)
To rank, there must be something similar to a score. If it doesn’t change frequently, just put it in the list and sort by score each time. If the ranking changes frequently or there are many players, maintain a maximum heap.
The player ranked m will be defeated by rank n, and the ranking will change
The general meaning is that all the rankings from n to m-1 are increased by 1, and then the original ranking of m is replaced by n.
If it is in the database, it can be easily processed by using sql twice. If it is stored in other text, it is easy to handle by writing a loop and increasing it by 1
I have done this function
I used to use ConcurrentSkipListMap<排名, uid>. Of course, if you don’t have concurrency requirements, you can also use TreeMap
Each player has a ranking value, because the ranking change only involves two players, only the ranking values of the two players need to be updated:
The advantage of using this is that it is very fast to search for a certain period of consecutive rankings (for example, to find a certain player and the players in front of him): map.subMap(from, to)
Do not use map or list, whether it is ArrayList or LinkedList, because ArrayList is equivalent to an array, and ranking changes require updating a large number of elements. Although LinkedList is more convenient for insertion and deletion, binary search cannot be used and it is necessary to find the changed elements. The location needs to be traversed. A better implementation is to use a balanced binary tree, or directly use sortset
in redisWhy use map? In this way, all the data after the inserted ranking must be adjusted every time. Isn’t it possible to use a list? Use list.add(int, object)
To rank, there must be something similar to a score. If it doesn’t change frequently, just put it in the list and sort by score each time. If the ranking changes frequently or there are many players, maintain a maximum heap.
The player ranked m will be defeated by rank n, and the ranking will change
The general meaning is that all the rankings from n to m-1 are increased by 1, and then the original ranking of m is replaced by n.
If it is in the database, it can be easily processed by using sql twice. If it is stored in other text, it is easy to handle by writing a loop and increasing it by 1
I have done this function
I used to use
ConcurrentSkipListMap<排名, uid>
. Of course, if you don’t have concurrency requirements, you can also useTreeMap
Each player has a ranking value, because the ranking change only involves two players, only the ranking values of the two players need to be updated:
The advantage of using this is that it is very fast to search for a certain period of consecutive rankings (for example, to find a certain player and the players in front of him):
map.subMap(from, to)
The ranking is fixed, just change the key values of the two players...