Collection/Set and Hashtable/HashMap
package com.Java程序设计入门.datastructure;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
/**
* Hashtable & Hashmap 属于 map。 Map 的 keySet() 返回所有的 key; values()返回所有 value; entrySet() 返回所有 key/value 对。
* Collection & Set 叫 集合 (Set extends Collection), 集合元素的遍历需要使用 Iterator。
* Iterator主要有三个方法 hasNext(), next() 和 remove()
* 其中 next() 是先跳跃元素然后返回他。也就是 next() 会返回值。由于 next() 返回的是 Object 类型,所以必须进行类型转化
* 这个例子是 map 与 collection 之间如何相互转化与遍历
*
* 另外,可以认为 HashMap 是 Hashtable 的简单版。 其别在于 HashMap 不是线性同步(Synchronize)的,而 Hashtable 是 Synchronize的。所以
* 在多线程下, Hashtable 是安全的,而 HashMap 则要引起注意。
*/
public class CollectionAndMap {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("One", "1");
map.put("Two","2");
map.put("Three","3");
Set keys = map.keySet();
Collection values = map.values();
System.out.println("Keys in the map are:");
Iterator iteratorKeys = keys.iterator();
while(iteratorKeys.hasNext()) {
System.out.println(iteratorKeys.next());
}
System.out.println("Values in the map are:");
Iterator iteratorValues = values.iterator();
while(iteratorValues.hasNext()) {
System.out.println(iteratorValues.next());
}
//////////////////
Hashtable table = new Hashtable();
table.put("China","CN");
table.put("United States", "USA");
table.put("Japanese", "JPN");
Set entries = table.entrySet();
Iterator iteratorEntries = entries.iterator();
System.out.println("entrySet in map are:");
while(iteratorEntries.hasNext())
{
System.out.println(iteratorEntries.next());
}
}
}
结果:import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
/**
* Hashtable & Hashmap 属于 map。 Map 的 keySet() 返回所有的 key; values()返回所有 value; entrySet() 返回所有 key/value 对。
* Collection & Set 叫 集合 (Set extends Collection), 集合元素的遍历需要使用 Iterator。
* Iterator主要有三个方法 hasNext(), next() 和 remove()
* 其中 next() 是先跳跃元素然后返回他。也就是 next() 会返回值。由于 next() 返回的是 Object 类型,所以必须进行类型转化
* 这个例子是 map 与 collection 之间如何相互转化与遍历
*
* 另外,可以认为 HashMap 是 Hashtable 的简单版。 其别在于 HashMap 不是线性同步(Synchronize)的,而 Hashtable 是 Synchronize的。所以
* 在多线程下, Hashtable 是安全的,而 HashMap 则要引起注意。
*/
public class CollectionAndMap {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("One", "1");
map.put("Two","2");
map.put("Three","3");
Set keys = map.keySet();
Collection values = map.values();
System.out.println("Keys in the map are:");
Iterator iteratorKeys = keys.iterator();
while(iteratorKeys.hasNext()) {
System.out.println(iteratorKeys.next());
}
System.out.println("Values in the map are:");
Iterator iteratorValues = values.iterator();
while(iteratorValues.hasNext()) {
System.out.println(iteratorValues.next());
}
//////////////////
Hashtable table = new Hashtable();
table.put("China","CN");
table.put("United States", "USA");
table.put("Japanese", "JPN");
Set entries = table.entrySet();
Iterator iteratorEntries = entries.iterator();
System.out.println("entrySet in map are:");
while(iteratorEntries.hasNext())
{
System.out.println(iteratorEntries.next());
}
}
}
----------------------
Keys in the map are:
One
Two
Three
Values in the map are:
1
2
3
entrySet in map are:
China=CN
Japanese=JPN
United States=USA
One
Two
Three
Values in the map are:
1
2
3
entrySet in map are:
China=CN
Japanese=JPN
United States=USA