验证中...
HashMap
Raw Copy
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* HashMap的三种遍历方式
* @author Administrator
*
*/
public class Test3 {
/**
* Map<Key,Value>
* @param args
*/
public static void main(String[] args) {
Map<String ,Hero> heros =new HashMap<String,Hero>();
for(int i =0;i<10;i++){
Hero h =new Hero(Integer.valueOf(i));
heros.put(h.toString(), h);
}
//1)遍历Key
Set<String> keys = heros.keySet();
for(String key :keys){
System.out.println(key);
Hero hero =heros.get(key);
}
//2)遍历Values
Collection<Hero> cs = heros.values();
for(Hero h :cs){
}
//3)遍历Entry
Set<Entry<String,Hero>> entrys = heros.entrySet();
for(Entry<String,Hero> entry:entrys){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
class Hero{
Integer id;
public Hero(Integer id){
this.id=id;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search