使用Java的小数据集的数据查找方法? [英] Data lookup method for small data set with Java?

查看:123
本文介绍了使用Java的小数据集的数据查找方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须根据三个输入数据字段查找一些数据。查找必须快。只有约20种可能的查找组合。我们已经使用静态HashMap实例实现了这一点,我们通过将三个数据字段相加来创建一个密钥。有没有更好的方法来做这个或者这样的方式吗?代码如下。



更新:我并不暗示这段代码很慢。只是好奇,如果有更好的方法来做到这一点。我认为可能会有一个更优雅的解决方案,但是如果没有引人注目的选择,我很乐意保持这一点。






创建类级静态HashMap实例:

  private static HashMap map = new HashMap(); 

我们如何将数据加载到内存中:

  private void load(Iterator iterator){
while(iterator.next()){
Object o = it.next();
key = o.getField1()+ - + o.getField2()+ - o.getField3();
map.put(key,o.getData());
}
}

我们如何查找基于三字段:

  private Stirng getData(String f1,String f2,String f3){
String key = f1 + - + f2 + - f3;
return map.get(key);
}


解决方案

你自己当然是足够快了因为除非你的应用程序需要加快速度,而且这是瓶颈,所以这并不重要。你所得到的是已经相当有效率了。



就是说,如果你想挤压这个例程的一切速度(不用汇编语言重写) ;-)你可能会考虑使用一个数组而不是一个 HashMap ,因为只有一小部分密钥。您必须开发某种哈希函数,将每个对象分别为0到19之间的唯一数字(或者实际上有许多元素)。您可能也可以优化该散列函数的实现,尽管我无法告诉您如何在不知道正在使用的对象的详细信息的情况下执行此操作。


We have to lookup some data based on three input data fields. The lookup has to be fast. There are only about 20 possible lookup combinations. We've implemented this using a static HashMap instance where we create a key by concatinating the three data fields. Is there a better way to do this or is this the way to go? Code is below.

Update: I'm not implying that this code is slow. Just curious if there is a better way to do this. I thought there might be a more elegant solution but I'm happy to keep this in place if there are no compelling alternatives!


Create class level static HashMap instance:

private static HashMap map = new HashMap();

How we load data into memory:

private void load(Iterator iterator) {        
    while (iterator.next()) {  
      Object o = it.next();
      key = o.getField1() + "-" + o.getField2() + "-" o.getField3();
      map.put(key, o.getData());
    }
}

And how we look up the data based on the three fields:

private Stirng getData(String f1, String f2, String f3) {
   String key = f1 + "-" + f2 + "-" f3;
   return map.get(key);
}

解决方案

Well, the question to ask yourself is of course "is it fast enough?" Because unless your application needs to be speedier and this is the bottleneck, it really doesn't matter. What you've got is already reasonably efficient.

That being said, if you want to squeeze every bit of speed possible out of this routine (without rewriting it in assembly language ;-) you might consider using an array instead of a HashMap, since there are only a small, limited number of keys. You'd have to develop some sort of hash function that hashes each object to a unique number between 0 and 19 (or however many elements you actually have). You may also be able to optimize the implementation of that hash function, although I couldn't tell you how exactly to do that without knowing the details of the objects you're working with.

这篇关于使用Java的小数据集的数据查找方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆