Java集合框架学习(十一) Hashtable详解
Hashtable介绍
1. 继承自Dictionary;
2. 线程安全;
3. 支持Iterator和Enumeration;
4. key和value都不可为null;
5. 一般用于多线程环境中。
类定义
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable
例子介绍
package com.dylan.collection;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class HashtableExample {
public static void main(String[] args) {
Enumeration names;
String key;
// Creating a Hashtable
Hashtable<String, String> hashtable =
new Hashtable<String, String>();
// Adding Key and Value pairs to Hashtable
hashtable.put("Key1","Chaitanya");
hashtable.put("Key2","Ajeet");
hashtable.put("Key3","Peter");
hashtable.put("Key4","Ricky");
hashtable.put("Key5","Mona");
names = hashtable.keys();
while(names.hasMoreElements()) {
key = (String) names.nextElement();
System.out.println("Key: " +key+ " & Value: " +
hashtable.get(key));
}
}
}
使用Iterator迭代HashTable
package com.dylan.collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
/**
* @author xusucheng
* @create 2018-02-03
**/
public class IterateHashtable {
public static void main(String[] args) {
String str;
// Creating a Hashtable instance
Hashtable<String, String> hashtable = new Hashtable<String, String>();
/* Adding key-value pairs to Hashtable
* public V put(K key, V value): Maps the specified key to the
* specified value in this hashtable. Neither the key nor the
* value can be null. The value can be retrieved by calling the
* get method with a key that is equal to the original key.
*/
hashtable.put("A", "Apple");
hashtable.put("B", "Orange");
hashtable.put("C", "Mango");
hashtable.put("D", "Banana");
hashtable.put("E", "Grapes");
System.out.println("Hashtable contains:");
/* public Set<K> keySet(): Returns a Set view of the keys
* contained in this map. The set is backed by the map,
* so changes to the map are reflected in the set, and
* vice-versa.
*/
Set<String> keys = hashtable.keySet();
//Obtaining iterator over set entries
Iterator<String> itr = keys.iterator();
//Displaying Key and value pairs
while (itr.hasNext()) {
// Getting Key
str = itr.next();
/* public V get(Object key): Returns the value to which
* the specified key is mapped, or null if this map
* contains no mapping for the key.
*/
System.out.println("Key: "+str+" & Value: "+hashtable.get(str));
}
}
}
Java集合框架学习(十一) Hashtable详解的更多相关文章
- Java集合框架学习(一)List
先附一张Java集合框架图. 从上面的集合框架图可以看到,Java集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Coll ...
- Java集合框架学习
集合框架 集合框架的目标 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现必须是高效的. 该框架允许 不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的扩展和适应必 ...
- Java 集合Collection与List的详解
1.什么是集合 存储对象的容器,面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式. 集合的出现就是为了持有对象.集合中可以存储任意类型的 ...
- java集合的方法及使用详解
一.java集合的分类及相互之间的关系 Collection接口:向下提供了List和Set两个子接口 |------List接口:存储有序的,存储元素可以重复 |------ArrayList(主要 ...
- 【java基础 11】java集合框架学习
导读:本篇博客主要是从整体上了解java的集合框架,然后主要介绍几个自己在项目中用到的结构,比如说:hashtable.hashmap.hashset.arraylist等! 一.宏观预览 从宏观上看 ...
- Java集合框架学习笔记
集合类的由来:对象用于封装特有数据,对象多了需要存储,如果对象的长度不确定,就使用集合存储. 集合特点1.用于存储对象的容器.2.集合的长度可变.3.集合中不可以存储基本类型 集合容器因为内部的数据结 ...
- Java后端框架之Spring Boot详解,文末有Java分布式实战项目视频可取
在 Java 后端框架繁荣的今天,Spring 框架无疑是最最火热,也是必不可少的开源框架,更是稳坐 Java 后端框架的龙头老大. 用过 Spring 框架的都知道 Spring 能流行是因为它的两 ...
- Java集合框架学习总结
转自:http://www.cnblogs.com/oubo/archive/2012/01/07/2394639.html Oubo的博客 以下介绍经常使用的集合类,这里不介绍集合类的使用方法,只介 ...
- 《Java Spring框架》SpringXML配置详解
Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...
- java里Struts2学习登录练习详解
最近在学struts2里面遇到很多错误,今天跟大家分享一下,我的开发工具是Eclipse: 1.到网上下载Struts2的包,这里不再累赘,百度有很多: 2.新建一个项目,记得后面加上web.xml文 ...
随机推荐
- [转帖]Oracle中unicode的几种不同字符编码模式
https://zhuanlan.zhihu.com/p/668340691# 在Oracle中unicode字符集中,存在以下几种不同unicode字符集的编码模式 AL32UTF8 UTF8 ...
- [转帖]TLS 1.2 浏览器兼容性
https://support-splashtopbusiness.splashtop.com/hc/zh-cn/articles/4414002633883-TLS-1-2-%E6%B5%8F%E8 ...
- [转帖]JVM随笔 --- 安全点(safe point)与 安全区域( safe region)
https://zhuanlan.zhihu.com/p/461298916 11 人赞同了该文章 最近回顾 JVM safe point 与 safe region 又有一些新的感悟与收获,特别写篇 ...
- 部分MySQL的SQL信息整理
模块补丁信息查看 select su as 补丁模块, count(1) as 数量 from gsppatchlog where TO_DAYS( NOW( ) ) - TO_DAYS(deploy ...
- [转帖]数据库系列之TiDB存储引擎TiKV实现机制
TiDB存储引擎TiKV是基于RocksDB存储引擎,通过Raft分布式算法保证数据一致性.本文详细介绍了TiKV存储引擎的实现机制和原理,加深对TiDB底层存储架构的理解. 1.TiDB存储引擎Ti ...
- [转帖]Kafka可靠性之HW与Leader Epoch
<深入理解Kafka:核心设计与实现原理>是基于2.0.0版本的书 在这本书中,终于看懂了笔者之前提过的几个问题 准备知识 1.leader里存着4个数据:leader_LEO.leade ...
- [转帖]sendfile“零拷贝”、mmap内存映射、DMA
https://www.jianshu.com/p/7863667d5fa7 KAFKA推送消息用到了sendfile,落盘技术用到了mmap,DMA贯穿其中. 先说说零拷贝 零拷贝并不是不需要拷贝, ...
- 计划任务方式定期获取jvm dump的方法
说明 产品最近有一些问题,想着能够每隔一段时间抓取一下dump文件. 需求 可以定期抓取, 需要注意磁盘空间的使用. 实现方法 定时任务使用 crontab 计划任务来做 预定义获取jvm dump的 ...
- Codeforces round 919 (div2)
Problem - A - Codeforces 暴力枚举 就可以: #include <bits/stdc++.h> #define int long long using namesp ...
- adb基础指令
1.查看连接的设备 adb.exe devices/adb devices device表示连接成功,offline表示设备未连接或未响应 2.进入命令模式,指定哪台设备 adb -s 序列号 ...