有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有时间值的java Hashmap,希望在特定秒处查找值

我有一个链接的散列图(我的查询按时间排序),其中包含来自数据库的股票数据,时间以午夜后(int)秒为单位,在MYSQL和Java中,值都是双值。我附上了下面的数据样本。我们在某一天获得的数据量各不相同,但通常接近10000个条目

30607131.46
30608131.44
30608131.45
30609131.46
30611131.48
30613131.49
30615131.51
30615131.5

我们正在绘制数据,我没有足够的声誉给你看一张图片,但它基本上看起来很像雅虎的财务图表。雅虎做的一件很酷的事情是,它检测到鼠标的位置,然后告诉你在某个特定时间图形的值(美元金额),我试图模仿这种效果。(由于其他原因,我们无法使用我们找到的任何免费绘图工具,而商业级的工具稍微超出了我们的价格范围

所以我有一个LinkedHashMap,我有一个函数,可以把一个像素的x值转换成一个近似时间(如果一个像素值超过一秒,则近似值)

public static final int getTimeFromPixel(Settings window, final int pixel) {
    return (int) Math.round((window.getTimeStart() + window.getXPixelValue()
        * (pixel - YAXIS)));
}

下面是我正在考虑的三个选项(两个是与下面类似的函数——都是伪代码和未编译的函数,但我试图使它们接近正确)

/**
 * Start at the beginning of my linkedhashmap "points" and then stop when the
 * key is as close as possible. Break the for loop when the keys are past
 * the time we are looking for since this is a linked hashmap and I know
 * that the times were placed into the linkedhashmap in order.
 * @param pixel_x the xvalue of a pixel (from a mouse event)
 * @return the value at our stock at the closest before time we have data for
 */
public double iterateThroughLinkedHashMapToFindValue(int pixel_x) {
    int pixelTime = Settings.getTimeFromPixel(window, pixel_x);
    int closestTimeWeHave = 0;
    for (int second : points.keySet()) {
        if (Math.abs(pixelTime - second)
                < Math.abs(pixelTime - closestTimeWeHave)) {
            closestTimeWeHave = second;
        }
        if (second > pixelTime) {
            break;
        }
    }
    return points.get(closestTimeWeHave);
}

/**
 * Start as close as possible to a key we want then start checking
 * backwards until we find a value we have. Since we get values almost
 * every 3 seconds, it shouldn't have to check very far to get a value.
 * @param pixel_x the xvalue of a pixel (from a mouse event)
 * @return the value at our stock at the closest before time we have data for    
 */
public double tryToGetCloseToValueInHashMap(int pixel_x) {
    // Go to the next pixel, subtract 1 second from that pixel (should put
    // us at the far end of our current pixel if a pixel represents more
    // than one second on our graph
    int pixelTime = Settings.getTimeFromPixel(window, pixel_x + 1) - 1;
    // count backwards until we find a time we have a value for.
    while (!points.containsKey(pixelTime)) {
        pixelTime--;
    }
    return points.get(pixelTime);
}

我的第三个选择是创建一个double[]数组,当我迭代通过点在屏幕上绘制它们时,基本上为屏幕上的每个像素创建一个值映射,这样我就可以调用这样的函数

 public double getValueFromPixel(int pixel_x) {
     return valueMap[pixel_x];
 }

我的问题是哪一个最好,如果有的话。如您所见,我经常获取数据(平均每3.5秒一次),因此从理论上讲,使用第二个选项在找到它可以使用的东西之前不需要扫描太远。值映射很好,但我是否希望在每次使用其他两个函数之一动态重新绘制或获取这些值时为每个点创建值映射

我一直通过谷歌搜索使用stackoverflow,但我找不到这个问题的好答案,所以我实际上在问一个。提前谢谢!如果有更好的办法,我洗耳恭听


共 (1) 个答案

  1. # 1 楼答案

    此作业不需要(链接的)HashMap。 使用一个NavigableMapTreeMap将是标准实现),它为这样的用例提供了许多有用的操作:floor/天花、higher/lower、first/last等等

    // get the value at this specific time or the last one before that
    Double valueAtTheTime = map.floorEntry(theTime).getValue();