Search by using java. lang. String that comes with java

Source: Internet
Author: User

Which of the following is better for searching and creating indexes using the matching function provided by java? Run it.

 

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 saves the file name and matches this book's object
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 ();
// For me, I didn't understand this sentence at the beginning. It turned out that I had been searching for a match, found one, hits and 1, and finally searched.
While (fromIndex = stringToSearch. indexOf (keyword, fromIndex + keyword. length ()))! =-1) // read more
{
Hits ++;
}
// Add the file name and matching times to HashMap
Rs. put (files [I]. getName (), new Integer (hits ));
}
Catch (IOException e)
{
E. printStackTrace ();
}
}
// Obtain the object
Iterator it = rs. keySet (). iterator ();
While (it. hasNext ())
{
String filename = (string) it. Next ();
Integer hits = (integer) Rs. Get (filename); // obtain the value corresponding to the file name
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 ("rabbit", "d:/textfolder ");

}

}

How to Use hashmap? Let's take a look at this example.

 

Package bag;
Import java. util .*;
// Key-value pairs are stored in HashMap and can be directly obtained through keys.
Class Hash
{
Hash ()
{
HashMap hash = new HashMap ();
Hash. put ("001", "Beijing"); // "001" is the key, and "Beijing" is the value.
Hash. put ("002", "Shanghai ");
Hash. put ("003", "Tianjin ");
Iterator it = hash. keySet (). iterator (); // This is the key object.
While (it. hasNext ())
{
System. out. println ("it. Next data value:" + get (it. next (); // obtain the value corresponding to the key.
}
}
}

Iterate. Iterator! How to use it? Compared with the following program, we should find that

 

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 ());
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.