map:键值对存储数据,key,value,key必须唯一
Collection:list,set,queue 注意 Collections不能实例化
HashMap里面有些可以操作的方法:
put(key,value):向HashMap中添加数据
可以通过实例化Collection将hashmap转化为hashset取出值和key
iterator():迭代器,可以迭代Collection实例
- 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 -->值:赵六