我想匹配数据并替换为新值,但是如果我在 HashMap 中放入超过 5908 行数据。它给出了错误。如何解决这个问题或其他解决方案?
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);
最佳答案
对于散列图,5908 没有任何意义。只有一个 64kB limit on the bytecode size of any one method .
您可以在多个方法中调用 map.put
,例如每个方法 5000 次调用;这不是特别干净,也不太容易维护。
您应该考虑将字符串和相应的替换内容放入外部存储,例如一个文件或一个数据库,在运行时加载到 HashMap 中。
关于java - 使用 HashMap 问题映射超过 5908 条数据线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346258/