package pkg13.pkg26;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Map<String, String> map = null; //宣告Map物件
map = new HashMap<String, String>(); //key和value是string類別
//增加內容
map.put("Gjun", "巨匠電腦");
map.put("Google", "http://www.google.com");
map.put("Yahoo", "http://www.yahoo.com.tw");
map.put("Pcschool", "http://www.pcschool.com.tw");
String val = map.get("Gjun"); //根據key求出value
System.out.println("val = " + val);
//將map的key值全部列出
//先宣告一個Set來接收key
Set<String> keys = map.keySet();
//利用Iterator介面,將集合內容輸出
//產生key的Iterator介面實體
Iterator <String> iter = keys.iterator();
//hasNext判斷有值時,列出值
while(iter.hasNext()){
String str = iter.next();
System.out.print(str + " ");
System.out.println(map.get(str));
}
}
}
run:
val = 巨匠電腦
Google http://www.google.com
Yahoo http://www.yahoo.com.tw
Gjun 巨匠電腦
Pcschool http://www.pcschool.com.tw
BUILD SUCCESSFUL (total time: 0 seconds)
留言列表