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 " );
}
}
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())); // 获得键所对应的值。
}
}
}
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());
}
}
}
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());
}
}
}