简体   繁体   中英

Comparing values using hashtable

There is a table like:

Code(starts with)     Value
1                       AAA
101D                    BBB
101DG                   CCC
2                       DDD

The above table has the columns code and corresponding values. The Code column signifies the string starts with the code given and on comparing it with the input code of the user, the corresponding value will be assigned from value column of table.

For example:

  • if the user's code is 100000, then value should be AAA......
  • For user's code 101D1111, value should be BBB(not AAA even if this starts with 1 as we will consider more significant comparison which is with 101D)......
  • For user's code as 101DG222, value should be CCC(not BBB even if it starts with 101D as we will notice the most significant comparison).....
  • For user cose 23333, value=DDD.....

I have put the following table in a hashtable with key as code column and value as value column.

HashTable hash= new HashTable();
hash.put("1","AAA");
hash.put("101D","BBB");
hash.put("101DG","CCC");
hash.put("2","DDD");

String comp="101D1111";//string to compare

Iterator itr= hash.entrySet().iterator();

while(itr.hasNext())
{
   Map.Entry e=(Map.Entry)itr.next();

  String key=  (String).getKey();

   //**Here logic is needed to compare comp and key and retrieve the corrsponding    value here as BBB**


}

Kindly help me with this logic.

You would have to first sort your entries by key length, with the longest ones being first and the shortest ones last. Because you first want to check whether your Code starts with 101DG , and only then check whether it starts with 101DG and finally 1 .

Then to check for matches you could use something like this:

if (comp.substring(0,key.length()).equals(key)) {
    // it's a match — use this Value
}

I don't think a HashTable is a good data structure for this purpose, since if you are iterating over all the entries, what's the point of having a HashTable ?

A better structure (which would take more work to implement, though) is a tree. Each node of the tree would represent a prefix and the value associated with it :

For example, this is how the tree would look for your example:

             ROOT
           /      \
         1 (AAA)   2 (DDD)
           |
           10
            |
           101
           /
        101D (BBB)
          |
        101DG (CCC)

Now, for a given user code, you start traversing the tree from the ROOT, each time following the child node that holds the next character of the user code. You stop when you don't find any child node that matches. Each node you visit, you take the value stored in it. You return the last value you find.

If you are still not versed with complex data structures that are present then,this simple solution will help you

String keySearch=new String();
String val=null;
for(i=0;i<comp.length();i++){
    keySearch+=comp.charAt(i);
    if(table.contains(keySearch)){
        val=map.get(keySearch);
    }
    else
        break;
}

but using a HashTable is not really efficient in this case

Below code would do the trick. You could also use Generics in the iterator to make the code type safe.

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;


public class TestClass {
public static void main(String[] args) {

HashMap hash= new HashMap<String, String>();


hash.put("1","AAA");
hash.put("101D","BBB");
hash.put("101DG","CCC");
hash.put("2","DDD");

String comp="101D1111";//string to compare

Iterator itr= hash.entrySet().iterator();

String value = null;
int length = 0;
while(itr.hasNext())
{
   Map.Entry e=(Map.Entry)itr.next();

   if(comp.startsWith(((String)e.getKey())) && ((String)e.getKey()).length() > length) {
       value = (String)e.getValue();
       length = ((String)e.getKey()).length();         
   }

   //**Here logic is needed to compare comp and key and retrieve the corrsponding    value here as BBB**


}
System.out.println(value);
}

}

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