现在的位置: 首页 > 综合 > 正文

map的put理解

2013年09月13日 ⁄ 综合 ⁄ 共 924字 ⁄ 字号 评论关闭
package test;

import java.util.Collections;
import java.util.*;

public class TestList {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1","hello");
map.put("2","world");
map.put("3","nihao");
map.put("4","shijie");
map.put("5","zhong");
map.put("6","guo");

Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = map.get(key);
System.out.println(key + "--" + value);
}

String str = (String)map.put("1", "linweihan"); //将索引为1的值给取代了,并返回旧的值
System.out.println(str);

System.out.println("==============================================");

Iterator iterator1 = map.keySet().iterator();
while (iterator1.hasNext()) {
Object key = iterator1.next();
Object value = map.get(key);
System.out.println(key + "--" + value);
}

}
}
=============================================

结果:

3--nihao
5--zhong
2--world
4--shijie
6--guo
1--hello
hello
==============================================
3--nihao
5--zhong
2--world
4--shijie
6--guo
1--linweihan

抱歉!评论已关闭.