Java代码
Map map = new HashMap();
Iterator it = mapentrySet(erator();
while (ithasNext()) {
MapEntry entry = (MapEntry) itnext();
Object key = entrygetKey();
Object value = entrygetValue();
}
JDK中应用新特性ForEach循环
Java代码
Map m = new HashMap();
for(Object o : mapkeySet()){
mapget(o);
}
返回的 set 中的每个元素都是一个 MapEntry 类型
private Hashtable<String String> emails = new Hashtable<String String>();
另外 我们可以先把hashMap 转为集合Collection再迭代输出不过得到的对象
//方法一: 用entrySet()
Java代码
Iterator it = emailsentrySet(erator();
while(ithasNext()){
MapEntry m=(MapEntry)itnext();
(email + mgetKey() + : + mgetValue());
}
// 方法二jdk支持用entrySet()和ForEach循环()
Java代码
for (MapEntry<String String> m : emailsentrySet()) {
(email + mgetKey() + : + mgetValue());
}
// 方法三用keySet()
Java代码
Iterator it = emailskeySet(erator();
while (ithasNext()){
String key;
key=(String)itnext();
(email + key + : + emailsget(key));
}
// 方法四jdk支持用keySEt()和ForEach循环
Java代码
for(Object m: emailskeySet()){
(email + m+ : + emailsget(m));
}
Map aa = new HashMap();
aaput(tmp new Object());
//追加 替换用同样的函数
aaremove(temp);
//删除
for (Iterator i = aavalues(erator(); ihasNext(); ) { Object temp = inext(); }
//遍历
来个完整的包含TreeSet的元素内部排序的
Java代码
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
HashMap<ObjectObject> hash = new HashMap<ObjectObject>();
TreeMap<ObjectObject> treeMap = new TreeMap<ObjectObject>();
listadd(a);
listadd(b);
listadd(c);
hashput( );
hashput( );
hashput( );
hashput( );
hashput( );
hashput( );
treeMapput( );
treeMapput( );
treeMapput( );
treeMapput( );
treeMapput( );
treeMapput( );
//list遍历
for(String m: list){
Systemoutprintln(m);
}
// hashmap entrySet() 遍历
for(MapEntry<ObjectObject> m: hashentrySet()){
Systemoutprintln(mgetKey()++mgetValue());
}
//hashmap keySet() 遍历
for(Object m: hashkeySet()){
Systemoutprintln(m++hashget(m));
}
// treemap keySet()遍历
for(Object m: treeMapkeySet()){
Systemoutprintln(m++treeMapget(m));
}
}