0

I want to read an input string , check whether it ends with a given list of values present in key part of the a property file say sample.properties given below and replace it with values :

(i.e: suppose input string ends with either COM or C.OM or COM. or COM, i want to replace it with COM)

sample.properties

ABC=COM    
BCD=COM    
EFG=COM    
HIJ=COM
KLM=CO
XYZ=CO

Note that i have around 4000 entries in the property file.

My plan is to :

1) load the key and values in a HashMap during loading of application.

2) look if the input String ends with any word contained in the "key" part of the property file. This would be done by taking the keySet of the HashMap.

Is it feasible , if, for every incoming input value, i check whether the input value ends with any value contained in the keySet of the Hashmap?

Or are there any other better way of doing it?

shwetha
  • 73
  • 1
  • 2
  • 10

2 Answers2

0
  1. Read the sample.properties file.
  2. Place each line in a String using String::replaceAll(String regex, String replacement).
  3. If there are more than one character to replace use a regex: [.,] will replace dots . and commas ,.
  4. Place it in your HashMap<String, String>
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Your HashMap is not doing you any good that way.


The simplest fast way to solve this problem is to remember all the different key lengths (there are probably less than 15). Then, for each length, if the input string is at least that long, then get the substring of that length from the end, and use Map.get() in your HashMap to get any replacement value for that key.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87