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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值