How to use java to find data in small data sets?

We have to find some data according to three input data fields Search must be fast There are only about 20 possible search combinations We do this using a static HashMap instance. We create a key by connecting three data fields Is there a better way to do this, or is this the way to go? The code is as follows

Update: I'm not saying this code is slow Just wondering if there is a better way to do this I think there may be a more elegant solution, but if there is no convincing alternative, I am happy to keep it!

Create a class level static HashMap instance:

private static HashMap map = new HashMap();

How do 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 find data based on the following three fields:

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

Solution

Well, the question to ask yourself is, of course, "is it fast enough?" Because unless your application needs to be faster, which is the bottleneck, it doesn't matter What you have is already quite effective

Having said that, if you want to squeeze all possible speeds out of this routine (don't rewrite it in assembly language; -), you may consider using arrays instead of hashmaps because there are only a limited number of keys You must develop a hash function that hashes each object as a unique number between 0 and 19 (or actually how many elements there are) You can also optimize the implementation of the hash function, but I don't know how to do this without knowing the details of the object you are using

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>