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 -->值:赵六