📄 fruitbasket.java
字号:
package HtreeExa;
import jdbm.RecordManager;
import jdbm.RecordManagerFactory;
import jdbm.helper.FastIterator;
import jdbm.htree.HTree;
//import jdbm.helper.Serializer;
import java.io.IOException;
import java.util.Properties;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
/**
* Sample JDBM application to demonstrate the use of basic JDBM operations.
*
* @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
* @version $Id: FruitBasket.java,v 1.3 2003/08/06 20:10:15 boisvert Exp $
*/
public class FruitBasket
{
RecordManager recman;
HTree hashtable;
FastIterator iter;
String fruit;
String color;
public FruitBasket()
throws IOException
{
/* Properties props = new Properties();
recman = RecordManagerFactory.createRecordManager( "H-Tree", props );
// create or load fruit basket (hashtable of fruits)
System.out.println( "Creating new root" );
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "Root", hashtable.getRecid() );
// Properties props = new Properties();
// recman = RecordManagerFactory.createRecordManager( "Tree", props );
System.out.println();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "Root", hashtable.getRecid() );
System.out.println( "Adding node to the Root..." );
hashtable.put( "arts", "arts" );
hashtable.put( "business", "business" );
hashtable.put( "science", "science" );
hashtable.put( "computer", "computer" );
hashtable.put( "regional", "regional" );
hashtable.put( "rediff", "rediff" );
showBasket();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "computer", hashtable.getRecid() );
System.out.println( "Adding nodes to the computer..." );
hashtable.put( "algorithem", "algorithem" );
hashtable.put( "artificial intelligence", "artificial intelligence" );
hashtable.put( "program mining", "program mining" );
hashtable.put( "robotics", "robotics" );
hashtable.put( "security", "security" );
hashtable.put( "software", "software" );
showBasket();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "security", hashtable.getRecid() );
System.out.println( "Adding nodes to the Security..." );
hashtable.put( "authentication", "authentication" );
hashtable.put( "hacking", "hacking" );
hashtable.put( "firewalls", "firewalls" );
hashtable.put( "secure programming", "secure programming" );
showBasket();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "rediff", hashtable.getRecid() );
System.out.println( "Adding nodes to the rediff..." );
hashtable.put( "jobs", "jobs" );
showBasket();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "jobs", hashtable.getRecid() );
System.out.println( "Adding nodes to the Jobs..." );
hashtable.put( "call center", "call center" );
hashtable.put( "accounting", "accounting" );
hashtable.put( "banking", "banking" );
hashtable.put( "marketing", "marketing" );
hashtable.put( "engineering", "engineering" );
hashtable.put( "teaching", "teaching" );
showBasket();
hashtable = HTree.createInstance( recman );
recman.setNamedObject( "hacking", hashtable.getRecid() );
System.out.println( "Adding nodes to the hacking..." );
hashtable.put( "conven verifier", "conven verifier" );
hashtable.put( "cracking", "cracking" );
hashtable.put( "cryptography", "cryptography" );
hashtable.put( "ethics", "ethics" );
showBasket();
recman.commit();*/
}
public ArrayList runDemo(String keyword)
throws IOException
{
//String keyword="IDS";
ArrayList data =new ArrayList();
ArrayList seq =new ArrayList();
seq.add(keyword);
Properties props = new Properties();
recman = RecordManagerFactory.createRecordManager( "H-Tree", props );
// System.out.println(props.keySet());
// create or load fruit basket (hashtable of fruits)
// System.out.println(recman.getRootCount());
long r=recman.getRoot(0);
// HashSet h=new HashSet();
// System.out.println(recman.fetch(r));
HashMap hm=(HashMap)recman.fetch(r);
for ( Iterator iterator = hm.entrySet ( ) .iterator ( ) ; iterator.hasNext ( ) ; ) {
Map.Entry entry = ( Map.Entry ) iterator.next ( ) ;
String key = ( String ) entry.getKey ( ) ;
// String value = ( String ) entry.getValue ( ) ;
// System.out.println(key);
data.add(key);
}
// if(hm.containsKey(keyword))
// {
// }
// else
// {
// int s=data.size();
//System.out.println("size"+s);
for(int i=0;i<data.size();i++)
{
//System.out.println("inter"+i);
long recid = recman.getNamedObject( (String)data.get(i) );
if ( recid != 0 ) {
hashtable = HTree.load( recman, recid );
iter = hashtable.keys();
fruit = (String) iter.next();
while ( fruit != null ) {
if(keyword.equals(fruit))
{
seq.add((String)data.get(i));
keyword=(String)data.get(i);
i=0;
i--;
// data.clear();
//data.add(keyword);
// System.out.print( " " + fruit );
// System.out.println((String)data.get(i));
// break;
}
// System.out.print( " " + fruit );
fruit = (String) iter.next();
}
}
}
// }
for(int i=0;i<seq.size();i++)
{
// System.out.println("keyword"+seq.get(i));
}
return seq;
}
public void showBasket()
throws IOException
{
// Display content of fruit basket
// System.out.println();
// System.out.print( "Fruit basket contains: " );
iter = hashtable.keys();
fruit = (String) iter.next();
while ( fruit != null ) {
// System.out.print( " " + fruit );
fruit = (String) iter.next();
}
// System.out.println();
}
public static void main( String[] args )
{
try {
FruitBasket basket = new FruitBasket();
basket.runDemo("software");
} catch ( IOException ioe ) {
ioe.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -