简体   繁体   English

在Java中为我的算法选择正确的数据结构

[英]Choose the right data structure for my algorithm in java

Today i need to choose the most efficent data structure for my needs in java. 今天,我需要为我在Java中的需求选择最有效的数据结构。 Basically i have an algorithm that have a set of <Integer, Object> . 基本上我有一个算法,它具有一组<Integer, Object>

  • First multiple thread create some of this set, than they get merged, sorted by the Integer and removed eventual duplicate Object in the result set. 首先,多个线程创建了该集合中的一部分,然后将它们合并,按Integer排序并在结果集中删除了最终的重复Object。
  • Than for each element x <Integer, Object> I need to Obtain the y elements with Integer less than x.Integer 对于每个元素x <Integer, Object>我需要获取Integer小于x.Integer的y元素。

Since the second point is the relevant in the algorithm which data structure would you choose to use in Java? 由于第二点与算法有关,因此您选择在Java中使用哪种数据结构?

You want a NavigableMap such as TreeMap or ConcurrentSkipListMap 您需要一个NavigableMap例如TreeMapConcurrentSkipListMap

Note: TreeMap is not thread safe, however is likely to be more efficient if you collect the data for each thread in a local copy of the TreeMap and merge the results when finished. 注意: TreeMap不是线程安全的,但是如果您在TreeMap的本地副本中收集每个线程的数据并在完成后合并结果,则可能会更有效。 ie you can have much lower contention. 也就是说,您可以降低竞争。

Note2: You only need a TreeMap for performing the less than comparison. 注意2:您只需要一个TreeMap执行less than比较。 A further improvement might be to use HashMap to collect the data in each thread, and merge the results into a TreeMap to perform the searchs. 进一步的改进可能是使用HashMap收集每个线程中的数据,然后将结果合并到TreeMap以执行搜索。

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

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