我想组织一组Events具有唯一性id和time. 我想Events在特定的时间范围内有效地查询。有些Events可能具有相同time但不同的id.
有些人Events可能有相同的time,所以番石榴TreeMultiSet类真的很接近我的需要。
但是,请考虑以下伪代码片段:
class Event
{
Object id;
long time;
}
TreeMultiSet<Event> s = TreeMultiSet.create( new Comparator<Event>( )
{
@Override
public int compare( Event o1, Event o2 )
{
if ( o1.time < o2.time )
{
return -1;
}
else if ( o1.time > o2.time )
{
return 1;
}
else
{
return 0;
}
}
});
s.add( new Event( "a", 0 ) );
s.add( new Event( "b", 0 ) );
s.add( new Event( "c", 0 ) );
在三个加法之后,TreeMultiSet将只包含Event3 次“a”,因为TreeMultiSet只考虑Comparator和三个Event对象具有相同的time。
我的第一个想法是合并id为我的Comparator实现的一部分,以区分Eventswith same time,但我ids没有自然顺序(它们只是Objects且不一定实现Comparable)。
subSet()这在进行,等...查询时也会很尴尬headSet()——我不希望我用来设置查询边界id的对象的 重要。Event
我可以从头开始实现一些定制的东西(或者,更有可能,将繁重的工作委托给一些底层集合),但我想确保那里没有我遗漏的东西。