`
chensl
  • 浏览: 56101 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Collection之映射表(Maps)

阅读更多

映射表(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;
}

 

分享到:
评论

相关推荐

    ognl语言指南

    对英文版使用手册的翻译,很全. ...1. 简介 2. 历史 3. 语法 4. 表达式 ...映射表(Maps) 对集合的投影(Project) 在集合中查询 查找第一个匹配项 查找最后一个匹配项 调用构造方法 调用静态方法 .....

    OGNL中文版详细文档

    映射表(Maps) 对集合的投影(Project) 在集合中查询 查找第一个匹配项 查找最后一个匹配项 调用构造方法 调用静态方法 读取静态域(Static Fields) 表达式计算 伪Lambda表达式 供集合使用的伪属性 与Java...

    hibernate 框架详解

    高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...

    最全Hibernate 参考文档

    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. 集合例子...

    Hibernate教程

    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. 集合...

    Hibernate3+中文参考文档

    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. 集合例子...

    Hibernate参考文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate 体系结构与配置 参考文档(html)

    高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate 中文 html 帮助文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    HibernateAPI中文版.chm

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate3.2中文文档(chm格式)

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate3.04中文文档.chm

    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. 集合...

    Hibernate中文详细学习文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate+中文文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    NHibernate参考文档 2.0.0 chm

    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. 可...

    NHibernate中文帮组文档(2008.11月更新)

    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. 可...

Global site tag (gtag.js) - Google Analytics