1. 程式人生 > >java中hashtable怎樣儲存資料和讀取資料

java中hashtable怎樣儲存資料和讀取資料

Hashtable-雜湊表類

以雜湊表的形式儲存資料,資料的形式是鍵值對.
特點:
查詢速度快,遍歷相對慢
鍵值不能有空指標和重複資料

建立
Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

添值

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

取值

String str=ht.get(1);
System.out.println(str);// Andy

對鍵進行遍歷

Iterator it = ht.keySet().iterator();

while (it.hasNext()) {
    Integer key = (Integer)it.next();
    System.out.println(key);
}

對值進行遍歷

Iterator it = ht.values().iterator();

while (it.hasNext()) {
    String value =(String) it.next();
    System.out.println(value);
}

取Hashtable記錄數

Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

int i=ht.size();// 7

刪除元素

Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

ht.put(1,"Andy");
ht.put(2,"Bill");
ht.put(3,"Cindy");
ht.put(4,"Dell");
ht.put(5,"Felex");
ht.put(6,"Edinburg");
ht.put(7,"Green");

ht.remove(1);
ht.remove(2);
ht.remove(3);
ht.remove(4);

System.out.println(ht.size());// 3


Iterator it = ht.values().iterator();

while (it.hasNext()) {
        // Get value
    String value =(String) it.next();
    System.out.println(value);
}

java中擷取字串到指定位置

String path="http://localhost:8080/Client/index.jsp";我現在要將最後一個斜槓後,'.'以前的部分(也就是'index')截取出來,該怎麼截?

    path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."))