1
我想匹配數據並用新值替換,但是如果我在HashMap中放置超過5908行數據。它給出了錯誤。如何解決這個問題或其他解決方案?使用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);