一个游戏项目,服务器需要维护一个玩家的有序集合(排行榜),玩家的一些动作会改变自身的状态,比如等级改变。我希望在不使用 Collections.sort()
方法的情况下维持这个集合的有序状态。
我尝试了继承了 TreeSet
然后实现一个重新排序的回调 ReorderCallback
,在任何玩家经验值改变的时候调用回调的方法 reorder()
来使集合(排行榜)保持有序,代码如下
interface ReorderCallback<T> {
void reorder(T element);
}
class AlwaysOrderedSet<T> extends TreeSet<T> implements ReorderCallback<T> {
// ...
@Override
public void reorder(T element) {
remove(element);
add(element);
}
}
然后调用 AlwaysOrderedSet
AlwaysOrderedSet<Player> set = new AlwaysOrderedSet<>();
player.setExp(xxxxx);
set.reorder(player);
然而,每次玩家状态改变后调用 reorder()
并不能保持原集合的有序,反而会重复添加 player。因为 TreeSet
无法追踪元素的变化,就像以下的演示一样,
public class Sorter {
public static void main(String[] args) {
class Student implements Comparable<Student> {
int id;
String name;
int age;
Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("id=%d, name=%s, age=%d", id, name, age);
}
@Override
public int compareTo(Student o) {
return o.age - this.age;
}
}
Set<Student> alwaysOrdered = new TreeSet<>();
Student a = new Student(1, "Amy", 50);
Student b = new Student(2, "Bob", 30);
Student c = new Student(3, "Chris", 40);
alwaysOrdered.add(a);
alwaysOrdered.add(b);
alwaysOrdered.add(c);
System.out.println("-- before --");
alwaysOrdered.forEach(System.out::println);
b.age = 100;
System.out.println("-- after --");
alwaysOrdered.forEach(System.out::println);
alwaysOrdered.remove(b);
alwaysOrdered.add(b);
System.out.println("-- after remove and add --");
alwaysOrdered.forEach(System.out::println);
}
}
结果是
-- before --
id=1, name=Amy, age=50
id=3, name=Chris, age=40
id=2, name=Bob, age=30
-- after --
id=1, name=Amy, age=50
id=3, name=Chris, age=40
id=2, name=Bob, age=100
-- after remove and add --
id=2, name=Bob, age=100
id=1, name=Amy, age=50
id=3, name=Chris, age=40
id=2, name=Bob, age=100
对 b 的更改并没有改变其在集合中的位置。移除 b 再添加 b 后反而元素变多了,即一开始就移除失败了。
所以我想问一下,有没有一种模式或者类能提供一种结构使得集合中元素值变化后,通过某种回调来使集合依旧有序?
You can remove the element first and then insert it.
The equals and hashCode methods of the class may need to be re-implemented.
Let’s not care about the details of using arrays or linked lists.
An array
int[] a=[10,6,2,0]
, when the value of the element a[3] changes from 0 to 7.int[] a=[10,6,2,0]
,当a[3]这个元素的值从0变成7的时候。做法可以如下:保持其它元素的相对位置不变(也就是不使用
Collections.sort()
),将a[3]这个元素放到a[0]后,然后将a[0]后的元素整体后移一位。看上不不错,但是考虑到这个是排名,比如说1000个用户,那么上面的操作的次数就要乘以1000。
这里是并发,肯定得涉及到加锁,所以性能可能并不乐观。
再想想我们自己玩游戏的体验,排行榜并不是实时刷新的。
🎜It doesn’t look good, but considering that this is a ranking, for example, if there are 1,000 users, then the number of operations above must be multiplied by 1,000. 🎜This is concurrency, which definitely involves locking, so the performance may not be optimistic. 🎜 🎜Think about our own experience of playing games. The rankings are not refreshed in real time. 🎜Then we still implement it through那我们还是通过
Collections.sort()
The method can be as follows: keep the relative position of other elements unchanged (that is, do not useCollections.sort()
), put the element a[3] after a[0], and then put a The elements after [0] are moved back one position as a whole.Collections.sort()
, once every 5 minutes, instead of modifying it every time the user information status changes. 🎜