利用java自带的java.lang.String搜索

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());
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值