-1

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 R1B1 (actully key is combination of R1+B2)

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

now for example lets say if i get ytr then i would be able to retrieve R1B1 or lets 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

lets 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

Folks please advise which will be the best data structure to obtain this functionality itself

erertg ghg
  • 89
  • 1
  • 2
  • 11

2 Answers2

2

I would use a Map. For the map entry, the key would be the matching sequence (abc) and the key would be the appropriate value (R1B1).

The same value can be placed in the map multiple times.

EDIT: Example

// Populating the search map
Value r1 = Value("r1");
Value b1 = Value("b1");
Value b2 = Value("b2");

MultiPartValue r1b1 = new MultiPartValue(r1, b1);
MultiPartValue r1b2 = new MultiPartValue(r1, b2);

Map<String, MultiPartValue> searchMap = new HashMap<>();

searchMap.add("abc", r1b1);
searchMap.add("def", r1b1);
searchMap.add("rty", r1b1);
searchMap.add("ytr", r1b1);
searchMap.add("dft", r1b1);

searchMap.add("abEERc", r1b2);
searchMap.add("dFFFef", r1b2);
searchMap.add("rGGty", r1b2);


// Using the search map
String searchText = "ytr";
MultiPartValue matchingValue = searchMap.get(searchText); // matchingValue = r1b1

The keys looked to me to be Strings; but the values R1B1 and R1B2 did not.

The classes MultiPartValue and Value are my modelling of your combinations of R1B1 and R1B2. Use what ever is appropriate for you use case. They could be Strings if that is appropraite.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
0

I guess you can use LinkedHashMap and once you insert the data it may look like as follows.

KEY             VALUE
abc             R1B1            
def             R1B1
rty             R1B1
ytr             R1B1
dft             R1B1
abEERc          R1B2
dFFFef          R1B2
rGGty           R1B2
Vikash Mishra
  • 635
  • 5
  • 7