java Hastable使用
jdk:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
Enumeration keys()
Returns an enumeration of the keys in this hashtable.
Set keySet()
Returns a Set view of the keys contained in this Hashtable.
Object put(Object key, Object value)
Maps the specified key to the specified value in this hashtable.
Collection values()
Returns a Collection view of the values contained in this Hashtable.
Object clone()
Creates a shallow copy of this hashtable.
boolean contains(Object value)
Tests if some key maps into the specified value in this hashtable.
boolean containsKey(Object key)
Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
Returns true if this Hashtable maps one or more keys to this value.
contains containskey区别,没什么区别,只不过containskey是很早以前的方法,contains是collection采用的方法。
官方说法:
contains:Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the collections framework).
遍历方法:
1.
private static void test() {
Hashtable<String,String> table = new Hashtable<String,String>();
table.put("1", "hello");
table.put("2", "world!");
Iterator i = table.entrySet().iterator();
while(i.hasNext()){
Entry entry = (Entry) i.next();
System.out.println(entry.getKey()+"=>"+entry.getValue());
}
}
table.entrySet返回的是一个set集合。集合里的元素为Entry。
第二种方法:
public class Hashtable1 {
Hashtable<String,String> table=new Hashtable<String,String>();
public void retrieve()
{
table.put("1","hello");
table.put("2","world!");
Enumeration e=table.keys();
while(e.hasMoreElements())
{
Object elem=e.nextElement();
System.out.println(elem+"=>"+table.get(elem));
}
}
Enumeration遍历方法:
- public interface Enumeration
An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.
For example, to print all elements of a vector v:
for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}
深入:
http://www.blogjava.net/fhtdy2004/archive/2009/07/03/285330.html
http://blog.csdn.net/teedry/article/details/4280034
java Hastable使用的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java集合类
1.Collection和Collections的区别? (1)Collection是一个接口,为集合对象的基本操作提供通用的接口放法. (2)Collections是一个工具类,里面包含各种对集合的 ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- Java集合类操作优化总结
清单 1.集合类之间关系 Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHas ...
- Java集合类操作优化经验总结
本文首先针对 Java 集合接口进行了一些介绍,并对这些接口的实现类进行详细描述,包括 LinkedList.ArrayList.Vector.Stack.Hashtable.HashMap.Weak ...
- Java多线程基础总结
一.线程和进程关系 二.创建方式1.继承Thread类,重写run方法2.实现Runable接口,重写run方法3.使用匿名内部类 三.API接口start()currentThread() 获取当前 ...
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...
- 75道阿里Java面试题,你能答上几道?
整理了下阿里近几年的java面试题目,大家参考下吧,希望对大家有帮助,可以帮大家查漏补缺. 答对以下这些面试题,可以淘汰掉 80 % 的求职竞争者. 1.hashcode相等两个类一定相等吗?equa ...
- 2018“金三”之一线互联网公司Java高级面试题总结
JVM 1.请介绍一下JVM内存模型??用过什么垃圾回收器都说说呗 2.线上发送频繁full gc如何处理? CPU 使用率过高怎么办? 如何定位问题?如何解决说一下解决思路和处理方法 3.知道字节码 ...
随机推荐
- Android手机安全软件的恶意程序检测靠谱吗--LBE安全大师、腾讯手机管家、360手机卫士恶意软件检测方法研究
转载请注明出处,谢谢. Android系统开放,各大论坛活跃,应用程序分发渠道广泛,这也就为恶意软件的传播提供了良好的环境.好在手机上安装了安全软件,是否能有效的检测出恶意软件呢?下边针对LBE安全大 ...
- tcpdump抓包并保存成cap文件
首选介绍一下tcpdump的常用参数 tcpdump采用命令行方式,它的命令格式为: tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ] [ -i 网络接 ...
- Create screenshots of a web page using Python and QtWebKit | Roland's Blog
Create screenshots of a web page using Python and QtWebKit | Roland's Blog Create screenshots of a w ...
- python下异常处理
1.python下异常如何处理: #encoding=utf-8 """ python遇到异常,程序直接运行 try: "判断有可能抛出异常的代码" ...
- hadoop的集群安装
hadoop的集群安装 1.安装JDK,解压jar,配置环境变量 1.1.解压jar tar -zxvf jdk-7u79-linux-x64.tar.gz -C /opt/install //将jd ...
- [Swust OJ 799]--Superprime Rib(DFS)
题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
- java键盘录入
System.out:标准输出设备(默认是:控制台) System.in:标准输入设备(默认是:键盘) --------------------- InputStream in = System.in ...
- Oracle Hint用法总结
1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...
- atexit模块解析
atexit模块很简单,只定义了一个register函数用于注册程序退出时的回调函数,我们可以在回调函数中做一些资源清理的操作. 注意回调函数只有正常退出的时候才会调用,如果程序是被信号杀死或者因为严 ...
- js中去除换行(\r\n)
解决方法:replace(/\r\n/g,"").replace("\n","") 测试: <script> var str = ...