您的位置:首页 > 其它

HashMap基本使用方法

2016-03-28 19:23 351 查看
Collection:list,set,queue
map:键值对存储数据,key,value,key必须唯一





hashMap:无序
treeMap:有序
demo:
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(101, "张三");
hm.put(102, "李四");
hm.put(103, "王五");
hm.put(106, "赵六");
Collection<String> c = hm.values();// 转化为hashSet类型
Iterator<String> ia = c.iterator();
System.out.print("姓名列表:");
while (ia.hasNext()) {
System.out.print(" " + ia.next());
}
System.out.print("\n学好列表");
Collection<Integer> c2 = hm.keySet();
Iterator<Integer> ia2 = c2.iterator();
while (ia2.hasNext()) {
System.out.print(" " + ia2.next());
}
//通过学号,查找值
for(Integer key:c2){
System.out.println("学号:"+key+" -->值:"+hm.get(key));
}
}

输出值:
姓名列表: 张三 李四 王五 赵六

学好列表: 101 102 103 106
学号:101 -->值:张三

学号:102 -->值:李四

学号:103 -->值:王五

学号:106 -->值:赵六
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: