2016-07-13 14 views
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); 

回答

3

關於哈希映射關於5908沒有任何意義。簡單地說有64kB limit on the bytecode size of any one method

您可以用多種方法將呼叫置於map.put,例如,每種方法5000次調用;這不是特別乾淨,也不是很容易維護。

您應該考慮將字符串和相應的替換物放入外部存儲區,例如,一個文件或數據庫,您可以在運行時將其加載到哈希映射中。