调试过了都能运行,源码见附件,了解本程序,基本上对hashtable的使用可以达到理解和基本使用。
晋级指南:了解对hashmap和hashtable的异同,包括线程安全,效率高低,hash值得计算不同。
作为java内util包最常用的类,hashtable基本上每个初级java程序员面试都要问到的题目,这里的小程序算个入门吧,有bug或错误请不吝指正,可以和本人联系shucsrain@gmail.com,QQ264185132
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Enumeration;
import java.util.Hashtable;
public class Main {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(in);
public static void main(String[] args) throws IOException
{
new Main();
}
public Main() throws IOException{
Hashtable htable = new Hashtable(20,0.75f);
datebase(htable);
}
public Main(Hashtable table) throws IOException
{
datebase(table);
}
public void datebase(Hashtable table) throws IOException{
int count=table.size();
int value,id,num;
String entry,key,data;
Enumeration keys =table.keys();
Enumeration elements = table.elements();
System.out.println(" Hashtable 简易数据库程序 ");
System.out.println("(1)输入数据");
System.out.println("(2)请除所有数据");
System.out.println("(3)显示单笔数据");
System.out.println("(4)删除单笔数据");
System.out.println("(5)显示所有数据");
System.out.println("(6)结束程序");
System.out.print("请输入您的选择:");
value = select();
switch(value)
{
case 1:
print("pls input a series of data :");
data = bf.readLine();
count++;
key=String.valueOf(count);
table.put(key, data);
print("input work done, pls entry any key to go on ." );
entry = bf.readLine();
new Main(table);
break;
case 2:
table.clear();
print("table has been all cleared .");
print("clear work done, pls entry any key to go on ." );
entry = bf.readLine();
new Main(table);
break;
case 3:
print("pls entry the id key you want to see :");
id = getid(count);
key =String.valueOf(id);
Object select = table.get(key);
data = select.toString();
print(" "+key+" "+data);
print("display work done, pls entry any key to go on ." );
entry = bf.readLine();
new Main(table);
break;
case 4:
print("pls entry the id key you want to remove");
id =getid(count);
key = String.valueOf(id);
table.remove(key);
count--;
num = count;
Hashtable tmp = new Hashtable(20,0.75f);
elements = table.elements();
table.clear();
while(elements.hasMoreElements())
{
key = String.valueOf(num);
tmp.put(key, elements.nextElement());
num--;
}
print("remove work done pls entry any key to go on");
entry =bf.readLine();
new Main(tmp);
break;
case 5:
String[] sortkeys = new String[count] ;
String[] sortdata = new String[count] ;
num=table.size();
keys = table.keys();
elements = table.elements();
while(elements.hasMoreElements())
{
sortkeys[num-1]= (String) keys.nextElement();
sortdata[num-1]= (String) elements.nextElement();
num--;
}
for(int i=0;i<count;i++)
print(" "+sortkeys[i]+" "+sortdata[i]);
print("currently there are "+count+" element in table");
print("entry any key to go on");
entry = bf.readLine();
new Main(table);
break;
default :
print("pls input valid selection :");
new Main(table);
}
}
public int select()
{
String input;
int value =0;
try {
input = bf.readLine();
value = Integer.parseInt(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
print("please select from 1 to 6 :");
select();
}catch(NumberFormatException e)
{
print("NumberFormatException");
select();
}
if(value >6|| value <=0)
{
print("please select from 1 to 6 :");
value = select();
}
return value;
}
public int getid(int count){
String input;
int value=0;
try {
input = bf.readLine();
value = Integer.parseInt(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(NumberFormatException e)
{
print("NumberFormatException");
value =getid(count);
}
if(value>count)
{
print("please enter valid number :");
getid(count);
}
return value;
}
public void print(Object o)
{
System.out.println(o);
}
}