入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

HashMap基本使用方法

创建时间:2016-03-28 投稿人: 浏览次数:848
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("
学好列表");
        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 -->值:赵六


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。