利用java內建的java.lang.String搜尋

來源:互聯網
上載者:User

java內建的匹配功能進行搜尋與建立索引進行搜尋哪個更好,運行一下吧

 

package ch2.lucenedmo.test2;

import java.io.*;
import java.util.*;

import org.apache.lucene.index.*;
import org.apache.lucene.search.*;

public class Search 
{
    private String INDEX_STORE_PATH="d:/index";
    
    public void StringSearch(String keyword,String searchDir)
    {
        File filesDir=new File(searchDir);
        File[] files=filesDir.listFiles();
        //HashMap儲存檔案名稱和匹配此書對
        Map rs=new HashMap();
        Date beginTime=new Date();
        for(int i=0;i<files.length;i++)
        {
            int hits=0;
            try
            {
                BufferedReader br=new BufferedReader(new FileReader(files[i]));
                StringBuffer sb=new StringBuffer();
                String line=br.readLine();
                while(line!=null)
                {
                    sb.append(line);
                    line=br.readLine();
                }
                br.close();
                String stringToSearch=sb.toString();
                int fromIndex=-keyword.length();
                //這句話對我來說,剛開始沒讀懂,原來是一直搜尋匹配,搜到一個,hits加1,最後搜完為止
                while((fromIndex=stringToSearch.indexOf(keyword,fromIndex+keyword.length()))!=-1)  //這個多看看
                {
                    hits++;
                }
                //將檔案名稱和匹配次數加入HashMap中
                rs.put(files[i].getName(),new Integer(hits));
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        //獲得檔案對象
        Iterator it=rs.keySet().iterator();
        while(it.hasNext())
        {
            String fileName=(String)it.next();
            Integer hits=(Integer)rs.get(fileName);//獲得檔案名稱所對應的值
            System.out.println("find"+hits.intValue()+"matches in"+fileName);
        }
        Date endTime=new Date();
        long timeSearch=endTime.getTime()-beginTime.getTime();
        System.out.println("the time for string search is"+timeSearch+"ms");
    }
    public static void main(String[] args) {
        Search search=new Search();
        search.StringSearch("兔子","d:/textfolder");

    }

}

 hashmap怎麼用,看看這個例子吧

 

package bag;
import java.util.*;
//HashMap 中存放的是索引值對,可以通過鍵直接取得其值。
class Hash
{
Hash()
{
HashMap hash=new HashMap();
hash.put("001","北京");//“001”為鍵,“北京”為值。
hash.put("002","上海");
hash.put("003","天津");
Iterator it=hash.keySet().iterator();//這是取得鍵對象
while(it.hasNext())
{
System.out.println("it.Next資料的值是:"+get(it.next())); //獲得鍵所對應的值。
}
}

Iterate這是什麼。迭代器嘛!怎麼用?對比下面的程式應該有所發現

 

import java.util.*;
public class Iterate {
  public static void main (String args[]) {
    String elements[] = {"Irish Setter", "Poodle", 
      "English Setter", "Gordon Setter", "Pug"};
    Set set = new HashSet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.