java

位置:IT落伍者 >> java >> 浏览文章

Java中怎样遍历Map的所有的元素


发布日期:2019年10月25日
 
Java中怎样遍历Map的所有的元素

JDK

view plaincopy to clipboardprint?

<FONT color=#ff>Map map = new HashMap();

Iterator it = mapentrySet(erator();

while (ithasNext()) {

MapEntry entry = (MapEntry) itnext();

Object key = entrygetKey();

Object value = entrygetValue();

}</FONT>

Map map = new HashMap();

Iterator it = mapentrySet(erator();

while (ithasNext()) {

MapEntry entry = (MapEntry) itnext();

Object key = entrygetKey();

Object value = entrygetValue();

}JDK应用新特性ForEach循环

view plaincopy to clipboardprint?

Map m = new HashMap();

for(Object o : mapkeySet()){

mapget(o);

}

Map m = new HashMap();

for(Object o : mapkeySet()){

mapget(o);

}返回的 set 中的每个元素都是一个 MapEntry 类型

view plaincopy to clipboardprint?

<FONT color=#ff>private Hashtable<String String> emails = new Hashtable<String String>();</FONT>

private Hashtable<String String> emails = new Hashtable<String String>(); 另外 我们可以先把hashMap 转为集合Collection再迭代输出不过得到的对象

view plaincopy to clipboardprint?

<FONT color=#ff>//方法一: 用entrySet()

Iterator it = emailsentrySet(erator();

while(ithasNext()){

MapEntry m=(MapEntry)itnext();

(email + mgetKey() + : + mgetValue());

}

// 方法二jdk支持用entrySet()和ForEach循环()

for (MapEntry<String String> m : emailsentrySet()) {

(email + mgetKey() + : + mgetValue());

}

// 方法三用keySet()

Iterator it = emailskeySet(erator();

while (ithasNext()){

String key;

key=(String)itnext();

(email + key + : + emailsget(key));

}

// 方法五jdk支持用keySEt()和ForEach循环

for(Object m: emailskeySet()){

(email + m+ : + emailsget(m));

}

</FONT>

//方法一: 用entrySet()

Iterator it = emailsentrySet(erator();

while(ithasNext()){

MapEntry m=(MapEntry)itnext();

(email + mgetKey() + : + mgetValue());

}

// 方法二jdk支持用entrySet()和ForEach循环()

for (MapEntry<String String> m : emailsentrySet()) {

(email + mgetKey() + : + mgetValue());

}

// 方法三用keySet()

Iterator it = emailskeySet(erator();

while (ithasNext()){

String key;

key=(String)itnext();

(email + key + : + emailsget(key));

}

// 方法五jdk支持用keySEt()和ForEach循环

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的元素内部排序的

view plaincopy to clipboardprint?

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));

}

}

               

上一篇:搜索引擎之中文分词实现(java版)

下一篇:Java Swing编程:JTree树