[英]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>
。
<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
例如TreeMap
或ConcurrentSkipListMap
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.