Map
package com.wangsu;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
public class TETSMAP {
static Map<Object,Object> map = new HashMap();
static{
for(int i=0;i<10;i++){
map.put(""+i,"a"+i);
}
}
@Test
public void TestMap(){
//方法一 遍历keySet
for (Object key : map.keySet()) {
System.out.println("key= "+ key+ " and value= " + map.get(key));
}
//方法二 迭代entrySet
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
System.out.println("key="+entry.getKey()+",value= "+entry.getValue());
}
//方法三 遍历entrySet
for(Map.Entry<Object, Object> entry: map.entrySet()){
System.out.println("key= "+entry.getKey()+",value="+entry.getValue());
}
}
}
List
package com.wangsu;
import java.util.ArrayList;
import java.util.Iterator;
public class TestList {
static ArrayList list = new ArrayList();
static {
for(int i=0;i<5;i++){
list.add(i);
}
}
public void Testlist(){
//迭代
Iterator it=list.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
//遍历
for(Object i:list){
System.out.print(i);
}
//遍历
for(int i=0;i<list.size();i++){
System.out.print(list.get(i));
}
}
public static void main(String [] args){
new TestList().Testlist();
}
}
来源:oschina
链接:https://my.oschina.net/u/2266231/blog/487672