简体   繁体   中英

container or data structure suitable to hold millisecond data

What would be a good data structure in Java to hold millisecond price data? About 1.000.000 elementsor more in the container. What is the specific container (vector, array, hash, etc) specifically designed to to hold a lot of data. I do not intend to filter or sort the data but I do need the ability to assign a key (possibly a tie stamp) to get the corresponding price.

If you need to assign a value (price) to a key (price), you should use a collection implementing the Map interface: TreeMap , HashMap , Hashtable or LinkedHashMap . You can find an overview of the collections here .

If a key can have more than one value, you will need a Multimap, ie a map where the value is a collection (see an example here ).

Assuming it is not the case, I'd suggest to use a TreeMap , ie a map where keys are sorted. Here is an example:

    Map<Long, Double> priceHistory = new TreeMap<>();

    ZonedDateTime zdt1 = ZonedDateTime.now();
    ZonedDateTime zdt2 = ZonedDateTime.of(
        LocalDate.of(2015,9,26),   
        LocalTime.of(11,10,9),
        ZoneId.systemDefault());

    priceHistory.put(zdt1.toInstant().toEpochMilli(), 10.5); 
    priceHistory.put(zdt2.toInstant().toEpochMilli(), 11.5);

It should not be a problem to hold 1 million elements and it should be fast to get the price for a given time stamp.

You probably don't really need millisecond precision, you just need to be able to get something for any given millisecond. Guava's RangeMap is great for this, you can construct a RangeMap<LocalDateTime, Price> and populate it with a value per second (for instance) with Range.closedOpen(second, second.plusSeconds(1)) as the key. You can then call .get() on an arbitrary LocalDateTime and it will return the value associated with the Range containing that time. You get arbitrary lookup precision but only use as much storage as you care to.

If you really do want millisecond precision you're going to have a hard time storing anything more than a few days of data efficiently. Instead you should look into storing your data in a database. Databases are designed to efficiently store and query large datasets, far beyond what standard in-memory data structures are designed for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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