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.知道字节码 ...
随机推荐
- 帝国cms本地搬家到服务器文章路径问题?
由于我的服务器不支持采集功能,我只能选择先在本地采集好文章发布于本地,再打算同步于服务器. 按照官方的做法, 1.先进后台备份了网站的所有数据,系统——备份与恢复数据——备份数据 2.将e\admin ...
- python下读取excel文件
项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...
- php 登录实例演示
<pre name="code" class="python">一.模板的使用 (重点) a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主 ...
- Hello China操作系统STM32移植指南(二)
移植步骤详解 下面就以MDK 4.72为开发环境,详细说明Hello China内核向STM32的移植过程.MDK 4.72评估版只支持32K代码的编译,这对Hello China的内核来说,裁剪掉一 ...
- PHP第一章学习——了解PHP(上)
计划开启PHP学习教程,情况如下: 1.采用教程35章48个视频文件 2.时间4月29日-5月6日 共计8天 3.具体划分每天学习章节数不少于5个,预留5-6号时间为五一假期出玩情况 4.要求认真学习 ...
- SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- Java多线程之synchronized(五)
上篇介绍了用synchronized修饰static方式来实现“Class 锁”,今天要介绍另一种实现方式,synchronized(class)代码块,写法不一样但是作用是一样的.下面我附上一段代码 ...
- Java InputStream读取网络响应Response数据的方法
Java InputStream读取数据问题 原理讲解 1. 关于InputStream.read() 在从数据流里读取数据时,为图简单,经常用InputStream.read()方法.这个方 ...
- Java学习之国际化程序
国际化程序就是把程序的语言根据用户使用的语言显示,各个国家的用户都可以看懂 实现方法就是把输出文字都写在配置文件里,然后根据用户系统语言选择不同的语言输出 package com.gh; import ...
- Linux 多线程通信
摘自资料(linux 与Windows不同) 线程间无需特别的手段进行通信,由于线程间能够共享数据结构,也就是一个全局变量能够被两个线程同一时候使用.只是要注意的是线程间须要做好同步,一般用mutex ...