映射表(map)用来存放键/值对,如果提供了键,就能够找到值,如一张员工信息记录表,键为员工ID,值为Employee对象。Java类库中为map提供了两个通用的实现:HashMap和TreeMap,都实现了Map接口。
下列代码为存储的员工信息建立一个散列映射表(HashMap):
Map<String, Employee> staff = new HashMap<String, Employee>(); // HashMap implements Map Employee harry = new Employee("Harry Hacker"); staff.put("987-98-9996", harry); . . .
要想检索一个对象,必须提供一个键,
String s = "987-98-9996"; e = staff.get(s); //gets harry,如果没有对应信息,返回null
remove方法用于从映射表中删除给定键对应的元素。size方法返回元素数。
集合框架没有将映射表本身视为一个集合(其他的数据结构框架则将映射表视为对(pairs)的集合,或视为用键作为索引的值的集合)。然而可以获得映射表的视图,这是一组实现了Collection接口对象,或者它的子接口的视图。
有3个视图,分别是键值、值集合(不是集)、和键/值对集。键与键/值对形成了一个集,这是因为在映射表中一个键只能有一个副本。下列方法将返回这3个视图(条目集的元素是静态内部类Map.Entry的对象)
(上段的英文原文如下:The collections framework does not consider a map itself as a collection. (Other frameworks for data structures consider a map as a collection of pairs, or as a collection of values
that is indexed by the keys.) However, you can obtain views of the map, objects that implement the Collection interface, or one of its subinterfaces.
There are three views: the set of keys, the collection of values (which is not a set), and the set of key/value pairs. The keys and key/value pairs form a set because there can be only one copy of a key in a map. The methods return these three views. The elements of the entry set are objects of the static inner class Map.Entry.)
Set<K> keySet() Collection<K> values() Set<Map.Entry<K, V>> entrySet()
注意,keySet既不是HashSet,也不是TreeSet,而是实现了Set接口的某个其他类的对象。Set接口扩展了Collection接口。因此,可以与使用任何集合一样使用keySet.
例如,可以枚举映射表中的所有键:
Set<String> keys = map.keySet(); for(String key : keys) { //do something with key }
如果想要同时查看键与值,就可以通过枚举各个条目查看,以避免对值进行查找。可以使用下面这段代码框架:
for (Map.Entry<String,Employee> entry: staff.entrySet()) { String key = entry.getKey(); Employee value = entry.getValue(); // do something with key,value }
下面程序显示了映射表的操作过程:
import java.util.*; /** * This program demonstrates the use of a map with key type String and value type Employee. * @author Cay Horstmann */ public class MapTest { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<String, Employee>();//首先将键/值对添加到一个映射表中 staff.put("144-25-5464", new Employee("Amy Lee")); staff.put("567-24-2546", new Employee("Harry Hacker")); staff.put("157-62-7935", new Employee("Gary Cooper")); staff.put("456-62-5527", new Employee("Francesca Cruz")); // print all entries System.out.println(staff); // remove an entry staff.remove("567-24-2546");//从映射表中删除一个键,同时与之对应的值也删除了 // replace an entry staff.put("456-62-5527", new Employee("Francesca Miller"));//修改于某一个键对应的值 // look up a value System.out.println(staff.get("157-62-7935"));//调用get方法,查看值 // iterate through all entries for (Map.Entry<String, Employee> entry : staff.entrySet()) { String key = entry.getKey(); Employee value = entry.getValue(); System.out.println("key=" + key + ", value=" + value); } } } /** * A minimalist employee class for testing purposes. */ class Employee { /** * Constructs an employee with $0 salary. * @param n the employee name */ public Employee(String n) { name = n; salary = 0; } public String toString() { return "[name=" + name + ", salary=" + salary + "]"; } private String name; private double salary; }
发表评论
-
Java添加UTF-7字符集支持(转)
2014-08-16 16:20 815这段时间在做PushServer时,需要对编码过的邮件标题及 ... -
问题记录
2012-05-02 20:58 451. 如何使java打印byte类型数据? 如: / ... -
利用java反射机制实现IoC容器
2011-07-05 17:44 0P717 -- P719 -
网址资源保存
2011-05-18 18:20 90xwork 下载地址: http://release.ope ... -
JAVA工具类(暂保存草稿,如果发表的话需要分别修改验证工具类内容)
2010-09-15 00:02 0(暂保存草稿,如果发表的话需要分别修改验证工具类内容) ... -
Collection之双端队列与优先级队列(Priority queue)
2010-09-06 10:28 1939双端队列 在Java SE6中,引入了Deque接口,并由A ... -
Collection之树集(TreeSet)
2010-09-04 23:50 1066TreeSet与HashSet类似,不过TreeSet是一个有 ... -
Collection之散列表(hash table)
2010-09-04 22:10 871链表和数组中元素是按一定次序进行排列的,散列表不在意元素的顺序 ... -
Collection之数组列表
2010-09-02 17:52 846数组列表(ArrayList)和上文中介绍的链表(Li ... -
Collection之链表
2010-09-02 11:00 870在Java语言中,所有的链表都是双向链接的(doubly li ... -
Concrete Collection集合概要
2010-09-02 00:22 749Java类库中的具体的集合: ArrayList 一种可以 ... -
泛型代码与虚拟机
2010-08-31 22:27 825虚拟机没有泛型对象,所有对象都属于普通类,使用当前Sun的编译 ... -
JAVA集合接口
2010-08-31 16:49 1246集合接口,JAVA集合类库 ... -
JAVA静态成员和静态内部类
2010-08-29 10:45 2146静态类(static class)即定义了静态方法,静 ... -
Java数据结构和算法--树(转)
2010-08-25 18:16 700(1)二叉树 class Tree { cl ...
相关推荐
对英文版使用手册的翻译,很全. ...1. 简介 2. 历史 3. 语法 4. 表达式 ...映射表(Maps) 对集合的投影(Project) 在集合中查询 查找第一个匹配项 查找最后一个匹配项 调用构造方法 调用静态方法 .....
映射表(Maps) 对集合的投影(Project) 在集合中查询 查找第一个匹配项 查找最后一个匹配项 调用构造方法 调用静态方法 读取静态域(Static Fields) 表达式计算 伪Lambda表达式 供集合使用的伪属性 与Java...
高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联(Ternary associations) 6.3.4. 使用 6.4. 集合例子...
7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联(Ternary associations) 6.3.4. 使用 6.4. 集合例子...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
1.1. 开始NHibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. 上下文相关的(Contextual)Session 3. 配置 3.1. 可...
1.1. 开始NHibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. 上下文相关的(Contextual)Session 3. 配置 3.1. 可...