searching the perfect data structure for storing and retrieving the elements

966 Views Asked by At

I have to choose one data structure for my need below i am explaining the conditions there are following values

abc,def,rty,ytr,dft   which all are map to row R1B1 (actully key is combination of R1+B1)
abEERc,dFFFef,rGGty   which all are map to row R1B2 (actully key is combination of R1+B2)


  KEY                      VALUE
abc,def,rty,ytr,dft --->    R1B1
abEERc,dFFFef,rGGty --->    R1B2

now, for example, let's say, if i get ytr then i would be able to retrieve R1B1 or, let's say, i get the value rGGty then i would be able to retrieve R1B2

now the case is that matters is of search, complexity and the time taken as the things have to go in sequence

for example, it will first pick the first line to search ytr, it will first match it with abc which will not match then will have to match with def it will not again match then it will match with rty which will not also match then it will finally match with ytr and finally it will find the key R1B1 finally

similarly if the second string need to be searched lets say rGGty then it would scan first row in which it will not find the value then search would continue to second row and also in second row in the third element it would get rGGty as element then it would retrieve R1B2 as value

let's say, if put this thing in map then a sequence search will go on key and then only we will be able to find the corresponding value

Folks please advise which will be the best data structure i can implement in java in which i will have to search the keys items to find the corresponding value in very fast time also which will not hit the performance too ,the kind of data structure performance should be very high

Please advise folks also it would be great if some one can advise me to show how this can be achieved using digital trees

2

There are 2 best solutions below

5
On

As the comments suggest you can simply use a HashMap or HashTable,
if you insist on implementing a structure on your own i would suggest a search trie.
In a search trie the number of maximum comparisons required to retrieve data is equal to the length of the key used, so to retrieve data for the given keys(abc,def,rty,ytr,dft) exactly 3 comparisons would be required
Consider the following wikipedia link: https://en.wikipedia.org/wiki/Trie
Also below is my quick and dirty implementation of a search trie (provided the keys are standard ASCII letters) :)-

public class DictionaryNode 
{
    DictionaryNode next[];
    String data;
    DictionaryNode()
    {
        next=new DictionaryNode[128];
        for(int i=0;i<next.length;i++)
        {
            next[i]=null;
        }
        data=null;
    }
}

public class Dictionary 
{
    DictionaryNode root;
    Dictionary()
    {
        root = new DictionaryNode();
    }
    public boolean add(String key,String data)
    {
        char[]ch=key.toCharArray();
        DictionaryNode current=root;
        for(int i=0;i<ch.length;i++)
        {
            if(current.next[ch[0]]==null)
            {
                current.next[ch[0]]=new DictionaryNode();
            }
            current=current.next[ch[0]];
        }
        if(current.data==null)
        {
            current.data=data;
            return true;
        }
        else
        {
            return false;
        }
    }
    public String search(String key)
    {
        char[]ch=key.toCharArray();
        DictionaryNode current=root;
        for(int i=0;i<ch.length;i++)
        {
            if(current.next[ch[0]]==null)
            {
                return null;
            }
            else
            {
                current=current.next[ch[0]];
            }
        }
        return current.data;
    }
}

public class main {
    public static void main(String []args)
    {
        Dictionary d=new Dictionary();
        d.add("abc", "R1B1");
        d.add("def", "R1B1");
        d.add("rty", "R1B1");
        d.add("ytr", "R1B1");
        d.add("dft", "R1B1");
        System.out.println(d.search("abc"));
        System.out.println(d.search("ab"));
    }
}
2
On

Inverse key and values in your case and use Multimap data structure. See good article here about Multimap. http://tomjefferys.blogspot.com/2011/09/multimaps-google-guava.html

@Test
public void getColor(){
    Multimap<String,String> myMultimap = ArrayListMultimap.create();


    myMultimap.put("R1B1","abc");
    myMultimap.put("R1B2","def");

    myMultimap.put("R1B2","abEERc");
    myMultimap.put("R1B2","rGGty");

    Multimap<String, String> invertedMultimap = Multimaps.invertFrom(myMultimap, ArrayListMultimap.<String, String>create());

    System.out.println(invertedMultimap.get("abc"));

}