10 Example of Hashtable in Java – Java Hashtable Tutorial
来源:互联网 发布:青岛海尔人工智能 编辑:程序博客网 时间:2024/05/29 15:30
http://javarevisited.blogspot.com/2012/01/java-hashtable-example-tutorial-code.html
importjava.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;
publicclass HashtableDemo {
publicstaticvoid main(String args[]) {
// Creating Hashtable for example
Hashtable companies = new Hashtable();
// Java Hashtable example to put object intoHashtable
// put(key, value) is used to insert object into map
companies.put("Google","United States");
companies.put("Nokia","Finland");
companies.put("Sony","Japan");
// Java Hashtable example to get Object fromHashtable
// get(key) method is used to retrieve Objects fromHashtable
companies.get("Google");
// Hashtable containsKey Example
// Use containsKey(Object) method to check if an Object exits as key in
// hashtable
System.out.println("Does hashtable contains Google as key: "
+ companies.containsKey("Google"));
// Hashtable containsValue Example
// just like containsKey(), containsValue returns true ifhashtable
// contains specified object as value
System.out.println("Does hashtable contains Japan as value: "
+ companies.containsValue("Japan"));
// Hashtable enumeration Example
// hashtabl.elements()return enumeration of all hashtable values
Enumeration enumeration = companies.elements();
while (enumeration.hasMoreElements()) {
System.out
.println("hashtable values: " + enumeration.nextElement());
}
// How to check if Hashtable is empty in Java
// use isEmpty method ofhashtable to check emptiness of hashtable in
// Java
System.out.println("Is companies hashtable empty: "
+ companies.isEmpty());
// How to find size ofHashtable in Java
// use hashtable.size() method to find size ofhashtable in Java
System.out.println("Size of hashtable in Java: " + companies.size());
// How to get all values formhashtable in Java
// you can use keySet() method to get a Set of all the keys ofhashtable
// in Java
Set hashtableKeys = companies.keySet();
// you can also get enumeration of all keys by usingmethod keys()
Enumeration hashtableKeysEnum = companies.keys();
// How to get all keys fromhashtable in Java
// There are two ways to get all values formhashtalbe first by using
// Enumeration and second getting values ad Collection
Enumeration hashtableValuesEnum = companies.elements();
CollectionhashtableValues = companies.values();
// Hashtable clear example
// by using clear() we can reuse an existinghashtable, it clears all
// mappings.
companies.clear();
}
}
Output:
Does hashtable contains Google as key: true
Does hashtable contains Japan as value: true
hashtable values: Finland
hashtable values: United States
hashtable values: Japan
Is companies hashtable empty: false
Size of hashtable in Java: 3
Read more: http://javarevisited.blogspot.com/2012/01/java-hashtable-example-tutorial-code.html#ixzz3Jd7e8SSF
0 0
- 10 Example of Hashtable in Java – Java Hashtable Tutorial
- java hashtable
- java Hashtable
- java-hashTable
- java HashTable
- Java- HashTable
- 【java】HashTable
- 4 example to Iterate over HashMap, Hashtable or any Map in Java
- How to traverse Hashtable in Java
- Difference between HashMap and Hashtable in Java
- Java Enum Tutorial: 10 Examples of Enum in Java
- List, HashSet; Hashtable, hashmap in java, dictionary in Csharp difference
- java.util.Hashtable翻译
- Java中的Hashtable
- java 中 Hashtable 排序
- Java Hashmap 和 Hashtable
- Java.Hashtable.Enumeration
- [zt] Java Hashtable分析
- JSP详细篇——out
- BestCoder Round #13 Beautiful Palindrome Number解题报告
- 创业周之行——公开课《商业模式的创新》
- 【学习】哈夫曼树——哈夫曼编码
- 高内聚低耦合的理解
- 10 Example of Hashtable in Java – Java Hashtable Tutorial
- Linux for sougou ping yin (http://pinyin.sogou.com/linux/help.php)
- MFC框架类、文档类、视图类相互访问的方法
- Linux 系统内核空间与用户空间通信的实现与分析
- 文章索引
- Netlink套接字 索引
- LayoutInflater的使用
- 『IOS』UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- QwtLegend解读---from Qwt-6.1.1