JAVA版hashtable的使用和学习,使用emuneration

调试过了都能运行,源码见附件,了解本程序,基本上对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);
 }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值