每条记录都有 ID、价格和数量。添加新记录时,如果 id 已存在,则新记录替换旧记录,id 在数据集中是唯一的。我经常需要获取排序数据(按价格)。比如数据集中的前 100 名或 25-50 名位置。好处是每次我需要排序数据时都是从数据集开始。 (当我可以的时候,我已经使用了 ComparablecompareTo(Object o))。
我尝试将数据存储到:
ArrayList 当 id 相同时替换元素或在末尾添加元素。当需要数据时对其进行排序。太慢了。
ArrayDeque 删除旧元素(如果存在)并添加新元素。当需要数据时对其进行排序。太慢了。
HashMap 有助于唯一 ID 和替换现有 ID。排序时,我从 HashMap 中获取值并将它们排序为 ArrayList。更快,但仍然不够。
TreeMap 有助于提供唯一 id,但无法按键对其进行排序。将值排序为 ArrayList。与 HashMap 相同。
SortedSet(TreeSet) 删除旧元素(如果存在)并添加新元素。使用迭代器获取已排序的元素。目前最快,但仍不够。
如果有人对使用什么数据类型/排序或如何使其尽可能快有任何建议。
我需要一些时间来完成此操作,例如,有 1 亿条记录已更新(添加/替换)并根据请求进行排序。在不使用任何基准测试软件的情况下,4Ghz 上的八核/四核(八线程 CPU)应该约为 2.3 分钟。请记住,随着记录数量的增加,添加/替换和排序的时间也会更长。
最佳答案
如suggested by László van den Hoek ,您应该考虑将数据实际存储在数据库中,该数据库可以处理大量数据,并且可以为数据建立索引以实现更快的排序查找。
但是,对于您自己开发的内存数据“存储”,您应该维护一个用于通过 id
查找的 HashMap
和一个 TreeSet
用于排序访问。
您没有指定您使用的排序方式,因此下面我假设您打算按价格
排序。
TreeSet
要求元素是唯一的,因此要按 price
排序,您还需要按 id
进行二次排序,以进行排序关键独特。 额外的副作用:具有相同价格
的记录的一致排序。
首先,我们完全定义您的 Record
类:
class Record implements Comparable<Record> {
private int id;
private double price;
private int quantity;
public Record(int id, double price, int quantity) {
this.id = id;
this.price = price;
this.quantity = quantity;
}
public Record(double price, int id) {
this.id = id;
this.price = price;
}
public int getId() {
return this.id;
}
public double getPrice() {
return this.price;
}
public int getQuantity() {
return this.quantity;
}
@Override
public int compareTo(Record that) {
int cmp = Double.compare(this.price, that.price);
if (cmp == 0)
cmp = Integer.compare(this.id, that.id);
return cmp;
}
@Override
public boolean equals(Object obj) {
if (! (obj instanceof Record))
return false;
Record that = (Record) obj;
return (this.id == that.id);
}
@Override
public int hashCode() {
return this.id;
}
}
备用构造函数是下面的DataStore
的便利助手。
DataStore
逻辑不需要 equals
和 hashCode
方法,但如果相等的话,实现它们总是一个好主意定义明确。
现在我们实现 DataStore
类,它封装了 HashMap
和 TreeSet
的逻辑:
class DataStore {
private Map<Integer, Record> recordsById = new HashMap<>();
private TreeSet<Record> recordsByPrice = new TreeSet<>();
public Optional<Record> addOrReplace(Record newRecord) {
Record oldRecord = this.recordsById.put(newRecord.getId(), newRecord);
if (oldRecord != null)
this.recordsByPrice.remove(oldRecord);
this.recordsByPrice.add(newRecord);
return Optional.ofNullable(oldRecord);
}
public Optional<Record> remove(int id) {
Record oldRecord = this.recordsById.remove(id);
if (oldRecord != null)
this.recordsByPrice.remove(oldRecord);
return Optional.ofNullable(oldRecord);
}
public Optional<Record> getById(int id) {
return Optional.ofNullable(this.recordsById.get(id));
}
public NavigableSet<Record> getByPrice(double price) {
return this.recordsByPrice.subSet(new Record(price, Integer.MIN_VALUE), true,
new Record(price, Integer.MAX_VALUE), true);
}
public NavigableSet<Record> getByPriceRange(double fromPriceInclusive, double toPriceExclusive) {
return this.recordsByPrice.subSet(new Record(fromPriceInclusive, Integer.MIN_VALUE), true,
new Record(toPriceExclusive, Integer.MIN_VALUE), false);
}
}
关于Java(openjdk版本 "1.8.0_151",linux)内存中有数百万条记录。经常添加和请求排序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49639361/