import java.util.*;
public class Map_key_value {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map1 = new HashMap<String,String>();
map1.put("h", "hizhu");
map1.put("z", "zhuhongbo");
Set<String> keys = map1.keySet();
for(String k:keys){
System.out.println("使用键-值方式输出:");
System.out.println("键:"+k+"\t值:"+map1.get(k));
}
for(Map.Entry<String,String> en:map1.entrySet()){
System.out.println("使用Entry方式输出:");
System.out.println("键"+en.getKey()+"\t值"+en.getValue());
}
System.out.println("******************************************");
Set<Map.Entry<String,String>> entry = map1.entrySet();
for(Map.Entry<String,String> en:entry){
System.out.println("使用Entry方式输出:");
System.out.println("键"+en.getKey()+"\t值"+en.getValue());
}
System.out.println("******************************************");
//当进行Map记数的时候可以这么做:
System.out.println("这个集合共有"+keys.size()+"对值!");
System.out.println();
System.out.println("******************************************");
}
}
这上面是Map,下面是Iterator和List:
import java.util.*;
public class MyIterator {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i<20;i++){
list.add(i);
}
System.out.println(list);
System.out.println();
System.out.println("************************************************************");
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.print(it.next()+"、");
}
System.out.println();
System.out.println("************************************************************");
// Iterator<Integer> it = list.iterator();
for(it = list.iterator();it.hasNext();){
System.out.print(it.next()+"、");
}
}
}