Java(openjdk版本 "1.8.0_151",linux)内存中有数百万条记录。经常添加和请求排序数据

标签 java sorting types

每条记录都有 ID、价格和数量。添加新记录时,如果 id 已存在,则新记录替换旧记录,id 在数据集中是唯一的。我经常需要获取排序数据(按价格)。比如数据集中的前 100 名或 25-50 名位置。好处是每次我需要排序数据时都是从数据集开始。 (当我可以的时候,我已经使用了 ComparablecompareTo(Object o))。

我尝试将数据存储到:

  1. ArrayList 当 id 相同时替换元素或在末尾添加元素。当需要数据时对其进行排序。太慢了。

  2. ArrayDeque 删除旧元素(如果存在)并添加新元素。当需要数据时对其进行排序。太慢了。

  3. HashMap 有助于唯一 ID 和替换现有 ID。排序时,我从 HashMap 中获取值并将它们排序为 ArrayList。更快,但仍然不够。

  4. TreeMap 有助于提供唯一 id,但无法按键对其进行排序。将值排序为 ArrayList。与 HashMap 相同。

  5. SortedSet(TreeSet) 删除旧元素(如果存在)并添加新元素。使用迭代器获取已排序的元素。目前最快,但仍不够。

    我已经检查了 Java Collections – 操作性能(时间复杂度)herehere .

如果有人对使用什么数据类型/排序或如何使其尽可能快有任何建议。

我需要一些时间来完成此操作,例如,有 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 逻辑不需要 equalshashCode 方法,但如果相等的话,实现它们总是一个好主意定义明确。

现在我们实现 DataStore 类,它封装了 HashMapTreeSet 的逻辑:

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/

相关文章:

Java 泛型 : how to declare a generic parameter as the base type of another generic parameter?

Haskell 数据类型列表

php - 排列多维数组

javascript - 按升序和降序对日期数组进行排序部分不正确的逻辑- Protractor

java - 如何子类化 Guava 的 ImmutableList?

java - 运行简单的 JSF 时出现异常?

arrays - Swift 3 - 检查数组中是否存在字符串并对其进行排序

vb.net - 获取List的类型

java - 运行 ping federate 无代理示例应用程序时出错

java - 标记上的语法错误、结构错误 需要代码帮助