Java - 基本类的使用(Map集合类)

来源:互联网 发布:朝鲜族吃里扒外知乎 编辑:程序博客网 时间:2024/03/29 20:58

6.24笔记

  • Map (字典)

    String key = “brand”;
    String key1 = “price”;

    /*HashMap允许key&value都是null,由于HashMap是非线程安全(轻量级),效率是较高的,常用的方法remove(); clear(); put();(没有排序要求的时候使用)LinkedHashMap是有序的;Hashtable不可以使用空值作key&value,Hashtable(重量级)在HashMap基础上封装了线程同步,也就是线程安全;*/HashMap map = new HashMap();map.put(key, "mac");map.put(key1, "9998");System.out.println(map);//遍历一个字典的key,valueSet maps = map.entrySet();Iterator it = maps.iterator();while (it.hasNext()){    System.out.println(it.next());}//遍历一个字典里面的keySet keys = map.keySet();Iterator it1 = keys.iterator();while (it1.hasNext()){    System.out.println(it1.next());}//遍历一个字典里面的valueCollection values = map.values();Iterator it2 = values.iterator();while (it2.hasNext()){    System.out.println(it2.next());}//根据key拿到valueString value = (String) map.get(key);System.out.println(value);
1 0