0

I have one lookup file like this

REGEXP|Name
^.*?AAA.*?$|POP
^.*?AAA.*?$|SOP
^.*?BBB.*?$|ZOP
^.*?CCC.*?$|ROP

I have input string and matching my input string with this lookup file with REGEXP column. And where ever it matches, Its returning Name.

Step 1 I'm loading lookup file in my tool like this

import java.util.Map;
import java.util.HashMap;

Map<String, String> regexMap = new HashMap<String, String>();

globalMap.put("regexMap", regexMap);

Step 2

Loading lookup file in hashmap like this

Map<String, String> regexMap = (Map<String, String>)globalMap.get("regexMap");
 if( row1.REGEXP != null ) {
  regexMap.put( row1.REGEXP, row1.Name);
}

Step 3

Hitting input string with this hashmap like this

Map<String, String> regexMap = (Map<String, String>)globalMap.get("regexMap");
for( String key : regexMap.keySet() ) {
 if( input_row.data != null ) {
  if( input_row.data.matches(key) ) {
   output_row.Name = regexMap.get(key);
   break;
  }
 }
}

I'm getting one value only. I want all values with matches with single key. Like in above mentioned lookup, If any string matches with first two REGEXP, output_row.Name should return POP and SOP both. Right now its returning only SOP.

Is any thing am missing like using Array list or doing something wrong.

Suggestion please?

kelly
  • 243
  • 3
  • 12

1 Answers1

0

A map can store multiple values per key ... but you'll need to use a different container (e.g. a Map of List, or a MultiMap) instead of a HashMap:

HashMap: One Key, multiple Values

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • @paulsm4...Is anything I could change my in this existing code ? Do you think if I change my Map structure like this Map> regexMap = (Map>)globalMap.get("regexMap"); and change the type of Name from string to List...will work ? – kelly Jan 16 '17 at 17:41
  • I believe so. The basic problem is that your unfortunate choice of "HashMap" will *NOT* work. Please review the thread for details. – paulsm4 Jan 16 '17 at 21:09