您的位置:首页 > 编程语言 > Java开发

Java Map 集合类简介

2008-06-24 11:28 519 查看
http://www.oracle.com/technology/global/cn/pub/articles/maps1.html
开发人员:J2EE

Java Map 集合类简介
作者:Jack Shirazi

了解最常用的集合类型之一 Map 的基础知识以及如何针对您应用程序特有的数据优化 Map。

本文相关下载:
· Jack 的 HashMap 测试
· Oracle JDeveloper 10g
------------------------------------

import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * 迭代 Map 中的元素不存在直接了当的方法,必须进行比较烦琐的编码.
 */
public class A {

	static Hashtable<String, String> my_Hashtable = new Hashtable<String, String>();
	public static void main(String[] args) {

		/**
		 * 利用循环语句为“哈希表”赋值
		 */
		for (int i = 0; i < 10; i++) {
			my_Hashtable.put("key" + i, "value" + i);
		}
		System.out.println("syso(Hashtable)" + my_Hashtable);

		System.out.println("==============开始:===================");
		// 有三种可能的视图:
		// 1. 所有键值对 — 参见 entrySet()
		// 2. 所有键     — 参见 keySet()
		// 3. 有值       — 参见 values()
                // 值得注意的是,这些对象(Set、Collection 和 Iterator)实际上是基础 Map 的视图,而不是包含所有元素的副本。 这使它们的使用效率很高。

		System.out.println("---------------以下为----遍历entrySet():Set视图:");

		//如果要查询某个Map,则您首先需要获取该 Map 的“视图”。 
		Set<Map.Entry<String, String>> se1 = my_Hashtable.entrySet();
		System.out.println("syso(Set<Map.Entry<String,String>>):" + se1);

		//您无法直接迭代视图。要进行迭代,您必须获得一个 Iterator 对象。
		Iterator<Map.Entry<String, String>> i5 = se1.iterator();
	        while (i5.hasNext()) {
			Map.Entry<String, String> entry = i5.next();
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println("Set<Map.Entry<String" + key + ", String"
					+ value + ">>");
		}
                
                 System.out.println("---------------以下为----遍历keySet():Set视图:");
		/**
		 * Returns a Set view of the keys contained in this Hashtable.
		 * The Set is backed by the Hashtable, so changes to the Hashtable are reflected in the Set, and vice-versa. 
		 * The Set supports element removal (which removes the corresponding entry from the Hashtable), but not element addition. 
		 */
		Set<String> se2 = my_Hashtable.keySet();
		System.out.println("syso(Set<String>):" + se2);
		Iterator<String> i1 = se2.iterator();
		while (i1.hasNext()) {
			String key = i1.next();
			String value = my_Hashtable.get(key);
			System.out.println("Set<String" + key + ">" + "-----" + value);
		}

		System.out.println("---------------以下为----遍历values():Collection视图:");
		/**
		 * Returns a Collection view of the values contained in this Hashtable. 
		 * The Collection is backed by the Hashtable, so changes to the Hashtable are reflected in the Collection, and vice-versa. 
		 * The Collection supports element removal (which removes the corresponding entry from the Hashtable), but not element addition. 
		 */
		Collection<String> c = my_Hashtable.values();
		System.out.println("syso(Collection<String>):" + c);
		Iterator<String> i2 = c.iterator();
		while (i2.hasNext()) {
			String value = i2.next();
			System.out.println("Collection<String " + value + ">");
		}

		System.out.println("---------------以下为----遍历keys():Enumeration枚举:");
		/**
		 * Returns an enumeration of the keys in this hashtable. //注:java.util.Hashtable$Enumerator@11b86e7
		 */
		Enumeration<String> e2 = my_Hashtable.keys();
		while (e2.hasMoreElements()) {
			String key = (String) e2.nextElement();
			System.out.println("Enumeration<String " + key + ">---"
					+ my_Hashtable.get(key));
		}

		System.out.println("---------------以下为----遍历elements():Enumeration枚举:");
		/**
		 * Returns an enumeration of the values in this hashtable.//注:java.util.Hashtable$Enumerator@35ce36
		 * Use the Enumeration methods on the returned object to fetch the elements sequentially.
		 */
		Enumeration<String> e1 = my_Hashtable.elements();
		while (e1.hasMoreElements()) {
			String value = (String) e1.nextElement();
			System.out.println("Enumeration<String " + value + ">");
		}

	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: