java |如何通过字典搜索模式

java |如何通过字典搜索模式,java,string,dictionary,search,pattern-matching,Java,String,Dictionary,Search,Pattern Matching,我的代码是按字典匹配模式(每个模式都有多个索引的所有字母),我的代码应该首先进入字典,检查模式相等索引dic,然后返回结果 我的问题是: 1-dic返回small、captel和number,但我只需要small和空格,我的文本文件只包含小词和空格 2-。花时间(例如:在文本文件+6000->10017中搜索“和”时) 我觉得这很慢,不确定 public static void search(JTextArea jTextArea2, String pat) {

我的代码是按字典匹配模式(每个模式都有多个索引的所有字母),我的代码应该首先进入字典,检查
模式
相等索引
dic
,然后返回结果

我的问题是:

1-
dic
返回small、captel和number,但我只需要small和空格,我的文本文件只包含小词和空格

2-。花时间(例如:在文本文件+6000->10017中搜索“和”时) 我觉得这很慢,不确定

 public static void search(JTextArea jTextArea2, String pat) 
    { 
    
         String text = jTextArea2.toString();  
            char[] textArray = text.toCharArray();

            //a dictionary that will hold the letter as the key and a list of it's positions as the value.
            HashMap<Character, ArrayList<Integer>> dictionary = new HashMap<Character, ArrayList<Integer>>();

            //loop through the text to check each letter
            for (int i = 0; i < textArray.length; i++) {            

                //if you've already checked this letter, skip to the next one
                if(dictionary.containsKey(textArray[i])) {
                    continue;
                }       

                //add the letter's position to its position list
                ArrayList<Integer> positionList = new ArrayList<>();
                positionList.add(i);

              //compare the remaining letters in the text to the current letter
                for (int j = i+1; j < textArray.length; j++) {

                        //if a letter matches, add it's position to the list
                        if(textArray[i] == textArray[j]) {
                        positionList.add(j);
                    }   
                }               

                //add the letter and its list of positions to the dictionary
              
                dictionary.put(textArray[i], positionList);
            }
           
            //format the response
            for(char key : dictionary.keySet()) {
                ArrayList<Integer> positions = new ArrayList<>();
                positions = dictionary.get(key);
                System.out.println(key + " has an occurance of " + positions.size() + " on positions: " + positions); 
                boolean flag = dictionary.containsKey(key);
                if (flag == true) {
                     System.out.println("result : \t"+ dictionary.containsKey(positions) );
                }
                }
            
            
公共静态无效搜索(JTextArea jTextArea2,字符串pat)
{ 
String text=jTextArea2.toString();
char[]textary=text.toCharArray();
//以字母为键,以字母位置列表为值的字典。
HashMap dictionary=新建HashMap();
//循环阅读文本,检查每个字母
对于(int i=0;i