简体   繁体   English

如何在Java中使用共享数据创建两个数组

[英]How to create two arrays with shared data in Java

First the reason I need to do this is because of the API in Apache's Commons StringUtils method: 首先,我需要这样做的原因是因为Apache的Commons StringUtils方法中的API:

StringUtils.replaceEach(String text, String[] searchList, String[] replacementList) 

What I want to do is replace all the HTML special character encodings with the actual special character, which means that the searchList and replacementList arrays will be pretty large. 我想要做的是与实际的特殊字符,这意味着替换所有的HTML特殊字符编码searchListreplacementList阵列将是相当大的。 How can I do this in an easy to read and maintain way? 我该如何以一种易于阅读和维护的方式来做到这一点?

Yes I could create two arrays but if I do this then it will be very easy to make mistakes. 是的,我可以创建两个数组,但是如果执行此操作,那么很容易出错。 How do I know I'm not missing a special encoding, that I have the right position, etc. I would much rather have code where the encoding and the character are side by side to avoid any errors. 我怎么知道我没有丢失特殊的编码,我的位置正确等等。 我宁愿有一些编码和字符并排的代码,以避免出现任何错误。 I looked at a HashMap but then you can only get the keys (encodings) and have to loop through to get the character values, which is not very performant, especially not if it's going to be run a lot. 我看了看HashMap但是然后您只能获取键(编码),并且必须循环获取字符值,这并不是很有效,尤其是如果要运行很多的话。 The same is true with a two dimensional array that you have to split each run. 对于必须拆分每次运行的二维数组,情况也是如此。

What type of performance are you aiming for? 您想要什么类型的表现? If you're looking to replace HTML special characters, can you not cache the result of splitting a HashMap of encodings to special characters in two static final variables of some sort? 如果您要替换HTML特殊字符,是否可以在某种形式的两个静态最终变量中缓存将HashMap编码拆分为特殊字符的结果? This will still require you to commit to the overhead of processing a HashMap, but saving the result prevents you from running the procedure every call. 这仍然需要您承担处理HashMap的开销,但是保存结果会阻止您在每次调用时都运行该过程。 Something like this: 像这样:

import java.util.HashMap;
import java.util.Map;

class MyStringReplaceCLass {
  private static final String[] encodings;
  private static final String[] specialCharacters;

  static {
      HashMap<String, String> characterEncoding = new HashMap<String, String>();
      characterEncoding.put("...", "...");
      characterEncoding.put("...", "...");

      // Put other encodings here as necessary

      encodings = new String[characterEncoding.size()];
      specialCharacters = new String[characterEncoding.size()];

      Map.Entry<String, String>[] entries = characterEncoding.entrySet();

      for (int i = 0; i < entries.length; i++) {
          encodings[i] = entries[i].getKey();
          specialCharacters[i] = entries[i].getValue();
      }
  }

  public String replaceEachEncoding(String text) {
      return StringUtils.replaceEach(String text, String[] searchList, String[] replacementList);
  }
}

From here, you can call 您可以从这里致电

MyStringReplaceClass.replaceEachEncoding(myText)

I'm not entirely sure if this meets your requirements exactly, but I feel a map of some sort with light processing would be the cleanest solution. 我不确定这是否完全符合您的要求,但是我认为某种经过轻加工的地图将是最干净的解决方案。

Say for text length of N, number of special characters M, searchList length of K. With HashMap, numbers of compares=N*K and number of exchanges M. 假设文本长度为N,特殊字符数为M,searchList长度为K。使用HashMap,比较数= N * K和交换数M。

For performance, 1. you may create a Tag for your search/replacement list. 为了提高性能,1.您可以为您的搜索/替换列表创建标签。 Then scan though text and Tag each entry (record the indices). 然后扫描文本并标记每个条目(记录索引)。 N compares. N比较。 2. Now you have M indices to replace with K possible characters. 2.现在,您有M个索引用K个可能的字符替换。 Compares = M K. Exchanges M. Compares N + M K < N*K. 比较= MK 。交换M。比较N + M K <N * K。 Exchanges M 交流M

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM