java - Java:优先队列> 比较器和实现
问题描述
我有一个名为 Event 的对象。我曾经将这些事件保存到:
Queue<Event> Q = new PriorityQueue<>(new EventCompare());
但现在我发现如果我有集合队列会更好:
Queue<Set<Event>> Q = new PriorityQueue<>(new EventCompare());
在我的比较器中,我比较事件的值。
private class EventCompare implements Comparator<Event> {
@Override
public int compare(Event e1, Event e2) {
if(e1.getValue() > e2.getValue()) {
return 1;
}
else if(e1.getValue() < e2.getValue()) {
return -1;
}
return 0;
}
}
现在我希望如果两个值相等,则在同一个集合中。所以队列应该是这样的:
{Event(1)}, {Event(12)}, {Event(15), Event(15)}.
我希望我已经足够清楚了。任何帮助,将不胜感激。
解决方案
推荐阅读
- html - 在 IOS 设备上自动缩放
- excel - 根据是条件将行数据从一个工作表复制到另一个工作表
- amazon-web-services - AWS Graphql 中的多对多关系问题
- docker - Docker 容器(用于弹性搜索)每次“[Qkskaso] 无法读取本地状态,正在退出...”时都会出现错误启动和退出
- android - 添加具有实时更新功能的 GeoJson(无跟踪)
- php - Mozilla Firefox 中的“资源被安全浏览阻止”问题
- ruby-on-rails - 避免在 ActiveRecord 中进行重复查询 - Ruby on Rails
- javascript - 在 JavaScript 中包含数组问题
- scala - 有没有办法在未来链中执行副作用,而不改变价值传播
- python-3.x - 如何将 int32 numpy 数组转换为 int16 numpy 数组?