javahashtable-30

问题描述:java中hashtable怎样存储数据和读取数据 大家好,小编来为大家解答以下问题,一个有趣的事情,一个有趣的事情,现在让我们一起来看看吧!

java中hashtable怎样存储数据和读取数据?

javahashtable-30的相关图片

Hashtable-哈希表类\x0d\x0a\x0d\x0a以哈希表的形式存储数据,数据的形式是键值对.\x0d\x0a特点:\x0d\x0a查找速度快,遍历相对慢\x0d\x0a键值不能有空指针和重复数据\x0d\x0a\x0d\x0a创建\x0d\x0aHashtable ht=new \x0d\x0aHashtable();\x0d\x0a\x0d\x0a添值\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0a取值\x0d\x0a\x0d\x0aString str=ht.get(1);\x0d\x0aSystem.out.println(str);// Andy\x0d\x0a\x0d\x0a对键进行遍历\x0d\x0a\x0d\x0aIterator it = ht.keySet().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a Integer key = (Integer)it.next();\x0d\x0a \x0d\x0aSystem.out.println(key);\x0d\x0a}\x0d\x0a\x0d\x0a对值进行遍历\x0d\x0a\x0d\x0aIterator it = ht.values().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a String value =(String) it.next();\x0d\x0a \x0d\x0aSystem.out.println(value);\x0d\x0a}\x0d\x0a\x0d\x0a取Hashtable记录数\x0d\x0a\x0d\x0aHashtable ht=new Hashtable();\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0aint i=ht.size();// 7\x0d\x0a\x0d\x0a删除元素\x0d\x0a\x0d\x0aHashtable ht=new Hashtable();\x0d\x0a\x0d\x0aht.put(1,"Andy");\x0d\x0aht.put(2,"Bill");\x0d\x0aht.put(3,"Cindy");\x0d\x0aht.put(4,"Dell");\x0d\x0aht.put(5,"Felex");\x0d\x0aht.put(6,"Edinburg");\x0d\x0aht.put(7,"Green");\x0d\x0a\x0d\x0aht.remove(1);\x0d\x0aht.remove(2);\x0d\x0aht.remove(3);\x0d\x0aht.remove(4);\x0d\x0a\x0d\x0aSystem.out.println(ht.size());// 3\x0d\x0a\x0d\x0aIterator it = ht.values().iterator();\x0d\x0a\x0d\x0awhile (it.hasNext()) {\x0d\x0a // Get value\x0d\x0a String value =(String) \x0d\x0ait.next();\x0d\x0a System.out.println(value);\x0d\x0a}。

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中HashMap和Hashtable分别是干什么用的?就是说他们有什么用途?什么时候用?的相关图片

Java中HashMap和Hashtable分别是干什么用的?就是说他们有什么用途?什么时候用?

就是哈希表,下面这个示例创建了一个数字的哈希表。它将数字的名称用作键: Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();  

 numbers.put("one", 1);  

 numbers.put("two", 2);  

 numbers.put("three", 3); 。

要获取一个数字,可以使用以下代码:

Integer n = numbers.get("two");  

 if (n != null) {  

 System.out.println("two = " + n);  

 }

java 多线程操作hashtable(添加,删除,遍历)的相关图片

java 多线程操作hashtable(添加,删除,遍历)

1 HashMap不是线程安全的 。

hastmap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值。HashMap允许null key和null value,而hashtable不允许。

2 HashTable是线程安全的一个Collection。

HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。

HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。

HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。

Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。

最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。

Java中的Hashtable如何根据值获取键?

public class TestThread {。

public static void main(String[] args){。

Map<Integer, Object> tables = new Hashtable<Integer, Object>();。

Thread add = new Thread(new ThreadAdd(tables));。

Thread del = new Thread(new ThreadDel(tables));。

Thread count = new Thread(new ThreadCount(tables));。

//启动线程

add.start();

del.start();

count.start();。

}

/**

*添加对象线程

*/

private static class ThreadAdd implements Runnable{。

private Map<Integer, Object> table;。

public ThreadAdd(Map<Integer, Object> tables){。

this.table=tables;。

}

public void run() {。

// TODO Auto-generated method stub。

for(int i=0;i<10;i++){。

table.put(i, new Object());。

System.out.println("添加对象,序号为:"+i);。

}

}

}

/**

*删除对象线程

*/

private static class ThreadDel implements Runnable{。

private Map<Integer, Object> table;。

public ThreadDel(Map<Integer, Object> table){。

this.table=table;。

}

public void run() {。

// TODO Auto-generated method stub。

for(int i=0;i<10;i++){。

try {

Thread.sleep(100);。

} catch (InterruptedException e) {。

// TODO Auto-generated catch block。

e.printStackTrace();。

}

table.remove(i);。

System.out.println("移除对象,序号为:"+i);。

}

}

}

/**

*统计线程

*/

private static class ThreadCount implements Runnable{。

private Map<Integer, Object> table;。

public ThreadCount(Map<Integer, Object> table){。

this.table=table;。

}

public void run() {。

// TODO Auto-generated method stub。

for(int i=0;i<10;i++){。

try {

Thread.sleep(50);。

} catch (InterruptedException e) {。

// TODO Auto-generated catch block。

e.printStackTrace();。

}

System.out.println("当前队列还剩"+table.size()+"个对象");。

}

}

}

这是我的写的demo,不知道符合不符合你的意思,大家共同交流共同进步。

原文地址:http://www.qianchusai.com/javahashtable-30.html

mafumafu-40

mafumafu-40

scalar,scalar multiplication

scalar,scalar multiplication

portman-110

portman-110

怡露饮品-50,怡露桶装水多少钱

怡露饮品-50,怡露桶装水多少钱

Sub,sub是什么意思字母圈

Sub,sub是什么意思字母圈

cc/胫骨平台骨折分型,胫骨平台骨折schatzkeriii型

cc/胫骨平台骨折分型,胫骨平台骨折schatzkeriii型

lw/社团活动图片,社团活动布置现场图片

lw/社团活动图片,社团活动布置现场图片

我是一个粗心的人作文,我是一个粗心的人作文怎么写

我是一个粗心的人作文,我是一个粗心的人作文怎么写

k大网课录屏,k大网课在哪里报名

k大网课录屏,k大网课在哪里报名

留心观察身边的事物作文,留心观察身边的事物作文三年级

留心观察身边的事物作文,留心观察身边的事物作文三年级

知网研学平台在线翻译工具 知网在线投稿靠谱吗 龙知网在线阅读 中国知网文献在线阅读 知网期刊在线阅读 中国知网在线学习平台怎么找 中国知网工具书库 中国知网没法在线阅读 知网阅读工具 知网免费下载神器 知网能够在线阅读嘛 知网论文在线复制方法 知网在线阅读价格 中国知网能在线阅读 知网在线服务平台 知网ai文献综述神器 知网阅读器推荐 怎么免费看知网文献在线阅读 知网可以在线阅读 小狗伪原创去除AIGC痕迹工具 觅知网图片在线编辑 知网ai综述写作神器防查重 知网文献阅读神器 除中国知网外的专利检索工具 抖音AI写作工具 中国知网期刊在线阅读 中国知网的外文不能在线阅读 中国知网不能在线免费阅读吗 知网免费图书馆在线阅读 知网在线教学平台