1

I would like to match the data and replace with new value but if I put more than 5908 lines of data in HashMap. It's given error. How to solved this issue or another solution?

String str = "aaa bob ccc ddd .....";
HashMap<String, String> map = new HashMap<>();
map.put("aaa","eee"); // data that I would like to match and replace it
map.put("ddd","fff");
.     
.     
.   // more than 5908 lines  
. 
.    
map.put("zzz"."yyy");

Pattern pattern = Pattern.compile("([a-zA-Z_]+)");
Matcher matcher = pattern.matcher(str) // matching process

StringBuffer sb = new StringBuffer();
while(matcher.find()) { // if found and replace
   String replace = map.get(matcher.group());
   if (replace == null) { // if not just print it back
      replace = matcher.group();
   }
   matcher.appendReplacement(sb, replace + "");
}
matcher.appendTail(sb);
System.out.println(sb);
Paranrax
  • 131
  • 7

1 Answers1

3

There is no signficance to the 5908 with respect to the hash map. There is simply a 64kB limit on the bytecode size of any one method.

You could put the calls to map.put in multiple methods, e.g. 5000 calls per method; that's not especially clean, and not very easy to maintain.

You should consider putting the strings and corresponding replacements into external storage, e.g. a file or a database, that you load into the hash map at runtime.

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243