简体   繁体   English

从许多HashMap对象创建SortedSet的最佳方法

[英]Optimal way of creating a SortedSet from a number of HashMap objects

I have a number of HashMap data structures containing hundreds of Comparable objects (say, of type MyClass ) and need to put all the values (not the keys) in a single data structure, and then sort it. 我有许多HashMap数据结构包含数百个Comparable对象(比如,类型为MyClass ),需要将所有值(而不是键)放在一个数据结构中,然后对其进行排序。

Due to the volume and the arrival rate of MyClass objects, this procedure (performed at least once per millisecond) needs to be as efficient as possible. 由于MyClass对象的体积和到达率,此过程(每毫秒至少执行一次)需要尽可能高效。

An approach would be to use SortedSet , roughly as follows: 一种方法是使用SortedSet ,大致如下:

HashMap<String, MyClass>[] allMaps = ... // All the HashMaps

SortedSet<MyClass> set = new TreeSet<MyClass>();

Collection<MyClass> c;

for (HashMap<String, MyClass> m:allMaps)
{
    c = m.values();
    set.addAll(c);
}

It may be faster to pass a sorted collection to set.addAll() , which may re-sort the TreeSet on every insertion, or after every few insertions. 将已排序的集合传递给set.addAll()可能会更快,这可能会在每次插入时或每次插入后对TreeSet进行重新排序。 However, to do so, a List needs to be passed to Collections.sort() , which means a conversion from Collection to List has to take place, ie another performance hit has to be sustained. 但是,为此,需要将List传递给Collections.sort() ,这意味着必须进行从CollectionList的转换,即必须维持另一个性能命中。

Also, there may be another, more efficient way of achieving the same goal. 此外,可能有另一种更有效的方法来实现相同的目标。

Comments? 评论?

I think the answer kinda depends on how the MyClass data tends to change. 我认为答案有点取决于MyClass数据如何变化。 For example, if you have a couple of new values coming in per timeframe, then you might want to consider keeping a hold of the last returned sorted set and a copy of the previous keys so that on the next run, you can just do a delta of the changes (ie find the new keys in the maps and manually insert them into the sorted set you returned last time). 例如,如果每个时间帧中有一些新值,那么您可能需要考虑保留最后返回的有序集和前一个键的副本,以便在下次运行时,您可以执行更改的增量(即在地图中查找新键并手动将它们插入上次返回的有序集中)。

This algorithm varies a bit if the MyClass objects might get removed from the maps. 如果可能从地图中删除MyClass对象,则此算法会有所不同。 But the general thought is to make it faster, you have to find a way to perform incremental changes instead of reprocessing the whole set every time. 但一般的想法是让它更快,你必须找到一种方法来执行增量更改,而不是每次都重新处理整个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM